Chitika

Monday, June 25, 2012

Checking is Async(partial) or full postback

It is common requirement to know wether user just opened page for the first time or he has done some action on the page which will cause a Async postback to the server.

Earlier to AJAX concept being implmented there is only complete page Postback, where all the information from the form was sent to the server, and completely page life cycle is exected and built html response is returned to the client.

But with AJAX in place, now we have a Partial (Asynchronous) Postback's, where specific part of the page is submitted to the server and only a part of the page is recreated and returned to the client, and part of the page is refreshed with the response from asynchronous request.

Partial refreshing of the page is what the main advantage of AJAX concept.
Here is how to determine this whether page is partially posted back or not: We can retrieve the instance of the ScriptManager class of the current Page via static GetCurrent method and inspect its IsInAsyncPostBackproperty to determine if Partial (Asynchronous) Postback has occurred:

Here is the example code:


protected void Page_Load(object sender, EventArgs e)
    {
        if (Page.IsPostBack)
        {
            // get a reference to ScriptManager and check if we have a partial postback
            if(ScriptManager.GetCurrent(this.Page).IsInAsyncPostBack)
            {
                // partial (asynchronous) postback occured
                // insert Ajax custom logic here
            }
            else
            {
                // regular full page postback occured
                // custom logic accordingly                
            }
        }
    }

No comments:

Post a Comment