Fredrik Normén's Blog - NSQUARED²
Any fool can write code that a computer can understand. Good programmers write code that humans can understand.

Microsoft Most Valuable Professional
     .Net Framework - ASP.Net - Architecture - Development
NOTE: This list of posts will only list the 15 latest posts, to see the rest, select from the Archives located in the menu to the left. The RSS will only list the 10 lastest posts.
Client-side callback changes

Category:  ASP.Net 2.0

Since I wrote the article about client-side callbacks some changes have been done. There is now two method that must be implemented when the ICallbackEventHandler is implemented, RaiseCallbackEvent ans GetCallbackResult. The reason to this is to allow asynchronous processing. The RaiseCallbackEvent will not longer return a string with the value of the result of a postback, instead the GetCallbackResult method will be called to return the result. RaiseCallbackEvent will first be executed, it’s where the argument form the client will be passed. When the RaiseCallbackEvent is executed the GetCallbackResult will be executed. The value return from the GetCallbackResult will be the value passed to the registered client-side method. So the code in my previous post will now look like:

 

<%@ page language="C#" %>

<%@ implements interface="System.Web.UI.ICallbackEventHandler" %>

 

<script runat="server">

 

        private string _returnFromServer;

 

    public void RaiseCallbackEvent(string eventArgs)

    {

        try

        {

            int value = Int16.Parse(eventArgs);

       

            if( value >= 1 && value <= 1000 )

                this. _returnFromServer = "You entered the number: " + eventArgs;

            else

                this. _returnFromServer = "Please enter a number between 1 and 1000";

        }

        catch

        {

            throw new ApplicationException("You must enter a number.");

        }

    }

 

    public string GetCallbackResult()

    {

                      return this._returnFromServer;

    }

  

    public void Page_Load(object sender, EventArgs e)

    {

       

        if (!Request.Browser.SupportsCallback)

                      throw new ApplicationException("This browser doesn’t support Client callbacks.");

 

        string src = Page.ClientScript.GetCallbackEventReference(

                                   this,

                                   "arg",

                                   "ClientCallback",

                                   "ctx",

                                   "ClientErrorCallback",

                                   false);

 

        string mainSrc = @"function ValidateNumber(arg, ctx)

                           { " + src + "; }";

 

        Page.ClientScript.RegisterClientScriptBlock(

                                   this.GetType(),

                                   "ValidateNumber",

                                   mainSrc,

                                   true);

    }

 

</script>

 

<html>

<head runat="server">

    <title>Untitled Page</title>

   

    <script language="javascript">

   

        function Validate()

        {

            var n = document.forms[0].txtNumber.value;

            ValidateNumber(n, "txtNumber");

        }

       

        function ClientCallback( result, context )

        {

            alert(result);

        }

       

        function ClientErrorCallback( error, context )

        {

            alert("The validation failed. " + error);

        }

   

    </script>

 

</head>

<body>

    <form runat="server">

      Please enter a number between 1 and 1000:<br />

      <input id="txtNumber" name="txtNumber" type="text"/>

      <button id="butVaidate" OnClick="Validate()">Validate</button>

    </form>

</body>

</html>

 

Posted: Saturday, December 03, 2005 - 09:24 GMT+1    Print     E-mail    Comments (5)
   fredrik.nsquared2.com - 2007