Page.IsCallback

IsPostback shows the page is posted back or loaded first time.
IsCallBack indicates whether the page request is the result of a call back.

Client Callback is a new feature in ASP.NET 2.0. It allows calling server side events asynchronously, without causing postback. It is possible to do asynchronous call using explicit AJAX, but in ASP.NET 2.0 and above versions a clean framework is available to do this. This article describes  how Client Callbacks works and also explores how Client Callback framework has been implemented in ASP.NET.


Consider in a page you have a textbox and a button. when the button is clicked,you need to display some complex content in the textbox(suppose that it takes 5 seconds).there are two ways to do this,By explicit Ajax,or Client Callback.the following is the second method.


Generating Client Side Code for Callback

For implementing callback in client side, a new method "GetCallbackEventReference" in Page Class is available. You can use this method to generate client side code which is required for the asynchronous call to server. In the example, client side code  is generated like this;


 1 <script>
 2     function getName(valuecontrol,msgControl)
 3     {
 4         msgControl.innerHTML = 'waiting';
 5         var valueID = valuecontrol.value;
 6         <%= ClientScript.GetCallBackReference(this,"valueID","receiveDateFromServer","msgControl"%>;
 7     }                    
 8     function receiveDateFromServer(result,context)
 9     {
10         context.innerHTML = result;
11     }
12 </script>
13 
14 <form id="form1" runat="server">
15     <div>
16         <asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>
17         <asp:Label ID="Label1" runat="server" Text="Label"></asp:Label>
18         <input id="Button1" type="button" value="button" onclick="javascript:getName(TextBox1,Label1);" />
19     </div>
20 </form>

this - The control that implements ICallbackEventHandler.(it is Current Page)

valueID - String to be passed to server side as argument.

ClientCallback - name of the client side function which will receive the result from server side event.

msgControl - a parameter in client side function, it will be back to other client side function.


String returned from the GetCallBackReference method will look like this:
 WebForm_DoCallback('__Page',valueID,receiveDateFromServer,msgControl,null,false);

WebForm_DoCallback is  another javascript function comes along with framework which will does the XMLHttp work for us. This function is located in the script returned by the WebResource.axd handler.


Creating Server Side Event for CallBack

Next part is in server side. If you want to implement call back in an ASP.NET page. Then that page has to implement ICallBackEventHandler interface. Basically it has to have RaiseCallbackEvent function which will be called during callback and GetCallbackResult function which will return a string result to client side. Here is the implementation of server side part for the example:

 1    private string strID = "";
 2
 3    public void RaiseCallbackEvent(string eventArgument)
 4    {
 5        strID += eventArgument;
 6    }

 7    public string GetCallbackResult()
 8    {
 9        Thread.Sleep(5000);
10        return string.Format("{0}'s name is QQ", strID);
11    }

the parameter valueID of client side function WebForm_DoCallback is passed to the parameter eventArgument of server side function RaiseCallbackEvent, and the parameter result of client side function receiveDataFromServer is the result of server side function GetCallBackResult.

at last,if you add this code snippets to server side,you will understand the Page.IsCallBack property:

1     protected void Page_Load(object sender, EventArgs e)
2     {
3         if (Page.IsCallback)
4         {
5             strID = "great ";
6         }
7     }
8 

end!

posted on 2009-09-15 12:32  武汉虫虫  阅读(1089)  评论(1编辑  收藏  举报

导航