Franc Stratton's .NET (TM) Web Application, OOP, and SOA Architecture & Programming Site

A site devoted to ASP.NET (TM), SilverLight (TM) and Browser-Based WPF (TM) Applications, IIS Services, and OOP Architectures

Home     Architecture Overview     WF/WCF/WPF     Data Store     Standards     .NET Security     Resources     jQuery     Silverlight     Developer Tips     Blog     Site Map      
Code Formatting
Code Notation
Passing Parameters
LINQ
AJAX
Database
SDLC (ALM)
ASP.Net Callbacks
Dirty Flag
ASP.NET Callbacks (without viewstate postback)
 
How to create an ASP.NET callback without viewstate postback:
 
1.  Add a reference to System.Web.UI.ICallbackEventHandler:  E.g.,
 
     public partial class MyfirstCallbackAttempt : System.Web.UI.Page, System.Web.UI.ICallbackEventHandler
 
2. In server page code-behind, you must create a method that implements the RaiseCallbackEvent event handler method and another event handler method that implements the GetCallbackResult method. The "System.Web.UI.ICallbackEventHandler" namespace interface requires these two methods. The RaiseCallbackEvent event handler method takes one string argument. E.g.,
 

    public void RaiseCallbackEvent(string eventArgument)

    {

        //The aStringValue is a declared protected string variable in code-behind

        this.aStringValue = "This is set in the RaiseCallbackEvent and sent to the arg variable in the ReceiveServerMethod client JavaScript from the GetCallbackResult method in code-behind.";

    }

 

3. Create a GetCallbackResult method takes no arguments and returns a string. E.g.,

   

    public string GetCallbackResult()

    {

        return aStringValue;

    }

 

4. Send the callback to the server page. This is accomplished in this example by the button onclick event. The Message span is to log the results when the string returns from the out-of-band callback. E.g.,
 

    <input type="button" value="Initiate the Callback From Onclick Event to registered CallServerMethod"  onclick="CallServerMethod(1, alert('Callback intiated from the button onclick event.'))"/>

    <br />

    <p></p>

    <span id="Message"></span>

 
5. Create a client-side JavaScript Function to receive the results.
 

<script type="text/javascript">

    function ReceiveServerMethod(arg, context)

    {

        // The receiving function accepts two string values: one for the return value and

        //an optional second value for the context value that is passed back from the server.

        //Although the context variable is passed to the function call, it is not passed

        //to the server-side method (the RaiseCallbackEvent method has only one parameter).

        //Instead, the context variable is cached in the browser throughout the entire

        //callback, and then passed as the second parameter to the returning JavaScript functions.

        //This allows the browser run script to identify the context of this entire callback.

        //Imagine a scenario of initiating several requests to the server that really serve

        //two separate events, or a case where there are several concurrent callbacks for

        //the same event. There won't be any guarantee that the returning JavaScript

        //methods are called in the same order in which the requests were initiated.

        //The context variable allows you to mark each request with a unique value

        //and act upon this value as it is being returned to the JavaScript functions.

        Message.innerText = arg.toString();

     }

</script>

 

6. Instantiate the client script manager (CSM) object Page.ClientScript. E.g.,

 

    ClientScriptManager csm = Page.ClientScript;

 
7. Create a reference to the client callback method created above. E.g.,

    //ClientScriptManager.GetCallbackEventReference obtains a reference to a

    //client function that, when invoked, initiates a client call back to a

    //server event. The client function for this overloaded method includes a

    //specified control or form, argument, client script, and context.

    //GetCallbackEventReference(Control, String, String, String, Boolean) The

    //boolean true to perform the callback asynchronously; false to perform

    //the callback synchronously.

    //See http://msdn.microsoft.com/en-us/library/system.web.ui.clientscriptmanager.getcallbackeventreference.aspx

    //For all 4 of the GetCallbackEventReference() overloads

    //The receiving function accepts two string values: one for the return value and

    //an optional second value for the context value that is passed back from the server.

    String callbackReference = csm.GetCallbackEventReference(this, "arg", "ReceiveServerMethod", "");

    String callbackScript = "function CallServerMethod(arg, context) {" + callbackReference + "; }";

   

8. When you have built the function, you inject it into the page by calling the RegisterClientScriptBlock method. E.g.,

    csm.RegisterClientScriptBlock(this.GetType(), "CallServerMethod", callbackScript, true);

 
That's it.