About Me

My photo
Northglenn, Colorado, United States
I'm primarily a BI Developer on the Microsoft stack. I do sometimes touch upon other Microsoft stacks ( web development, application development, and sql server development).

Tuesday, September 11, 2007

Quick reminder: Implement ICallbackEventHandler

A quick reference on how to implement ICallbackEventHandler instead of using the new Ajax, do it old-school style.

The page source (client-side):

<script language="javascript" type="text/javascript">
function Save()
{
   var message = '';
   var context = '';
<%=sCallBackFunctionInvocation%>
}
function Result(result, context)
{
   if(result != 'ok')
   alert("Error: " + result + "\n\nFile was not saved.");
}
function OnError(message, context)
{
   alert('An unhandled exception has occurred:\n' + message);
}
</script>


The code-behind (server-side):

public partial class EDCWUC : System.Web.UI.UserControl, ICallbackEventHandler
{
   public string sCallBackFunctionInvocation;
   protected void Page_Load(object sender, EventArgs e)
   {
   sCallBackFunctionInvocation = this.Page.ClientScript.GetCallbackEventReference(this, "message", "Result", "context", true);

   }

   #region ICallbackEventHandler Members
   public string GetCallbackResult()
   {
      return sCallBackFunctionInvocation;
   }

   public void RaiseCallbackEvent(string eventArgument)
   {
      sCallBackFunctionInvocation = DoServerSideEvent();
   }
   #endregion
}

A good hint: If you need to do several callbacks, then set it up so that the eventArgument sends in the function name with its values, then all you need to do is split it, get the function name and send in the necessary values
For example:
FunctionName;<uniqueDelimeter>;Values

No comments: