A week ago I wrote about the native client callback feature of ASP.NET 2.0 and why it isn’t always the smart choice. That post got me some good feedback that inspired me to build a custom callback implementation so you wouldn’t have to use the native one. I could just use other AJAX libraries such as ASP.NET AJAX or Ajax.NET, but that’s not fun.
- Uses GET instead of POST for performance reasons
- Doesn’t send the ViewState to eliminate the overhead
- Simple implementation
Because it doesn’t send the ViewState you are not able to check the state of the controls on the page. You can use the Session and Cache and any other store, just not the ViewState. You cannot implement it on custom server controls, because it only works on pages.
Step 1 - change inheritage
In the page you want to do client callbacks, just inherit from CallbackPage instead of System.Web.UI.Page and override the IncommingCallback(string argument, string context) method. Here is an example of such a page’s code-behind file.
public partial classdefault : CallbackPage
protectedoverridevoid IncomingCallback(string argument, string context)
if (context == "verify")
if (argument == "william")
Example of use
if (response == "true" )
alert( "You are verified" );
alert( "You are not verified" );
</ script >
Step 2 - Call the method
< asp : TextBox runat ="server" ID ="txtName" />
onclick ="CreateCallback(NameVerifier, document.getElementById(' <%=txtName.ClientID %> ').value, 'verify')"
value ="Create callback" />
The SetCallbackResponse(string argument) server-side method sends the argument parameter back to the client – if it isn’t called, nothing is sent back.