Callbacks in a custom Controls
Autor: Erick Almeida | Publicado: 2007-07-20T16:10:00-03:00

   Callbacks  Callbacks allow a control or page to perform an out-of-band call back to the server in a manner that does not require the page to be posted back entirely. This means that your custom control does not cause the client browser to wait on an entire page post back and your custom control can enable rich UI functionality. You can develop a custom control that easily combines for example, web resources with client script management to make use of the call back infrastructure. To enable a call back you will:         Create a server-side call back event with a given signature in your custom     control, (ICallbackEventHandler.PrepareCallbackEvent) that acts on an argument passed into the method&lt;. You     should call on a method that developers can override if they extend your     custom control;           Create a server-side method that generates the result for the call back,     ICallbackEventHandler.RenderCallbackResult. You should call a method that developers can override if they extenf     your control. The return result will be passed to client-side script defined     by your custom control;           Create client-side script that manages the return call or error. This will     be generated through your custom control;           Use the callback event reference hooked up to a client-side event in your     custom control.                      public class CustomControl : CompositeControl     void ICallbackEventHandler.PrepareCallbackEvent(string    eventArgument)     {             PrepareCallBackEvent(eventArgument);     }          String ICallbackEventHandler.RenderCallbackResult()     {             return RenderCallbackResult();     }          private String _callbackEventArg;          // Do work in a protected virtual methods so that derived controls can     override     protected virtual void PrepareCallBackEvent(string eventArgument)      {             _callbackEventArg = eventArgument;     }          protected virtual String RenderCallbackResult()     {             if (_callbackEventArg == "theArg")                 return = "Some     data";             return "Only theArg allowed!";     }          // Additional implementation          // Client script functions that handle the callback     private String sButtonCallBack =    "function     ButtonCallBack(result, context) { alert(result); }";     private String sButtonCallBackError =    "function     ButtonErrorCallBack(result, context) { alert(result); }";          protected override void OnInit(EventArgs e)     {     base.OnInit(e);          // Additional implementation          Page.ClientScript.RegisterClientScriptBlock(typeof(CustomControl),     "ButtonCallBack", sButtonCallBack, true);     Page.ClientScript.RegisterClientScriptBlock(typeof(CustomControl),     "ButtonCallBackError", sButtonCallBackError, true);     }          // Additional implementation          protected override void OnPreRender(EventArgs e) {     // Set up the OnClick event to fire an out-of band call to the handler     Attributes["OnClick"] = Page.ClientScript.GetCallbackEventReference(this,     "'theArg'", "ButtonCallback", "null", "ButtonErrorCallback", true);     base.OnPreRender(e);     }     }