Main FAQ Category: ASP.NET AJAX (8)
How to determine whether an Asynchronous Partial Postback has occurred on page?
Posted: 18-Apr-2008
Updated: 18-Apr-2008
Views: 33461

It is necessary to know if user just opened our ASP.NET page for the first time, or he has pressed some submit controls and caused page Postback to send information or commands to the server.

Before Ajax came along there was only one type of Postback - a regular, full Postback of the page to the server, where all the information from the form was sent to the server, and completely new ASP.NET page was generated and its HTML code was returned to the users web browser to render it and completely replace the old page.

With Ajax we now have a Partial (Asynchronous) Postback, where specific information or command is sent to the server, where only a necessary part of the page is re-generated and returned to the user, and this new part of the page is inserted dynamically into the previous page contents, without a full reload of the page.

This is off course a major advantage of the new Ajax model over the old approach.

Since there are now two types of Postbacks we really need to know what type of Postback occurred on our page.

Here is how to determine this: We can retrieve the instance of the ScriptManager class of the current Page via static GetCurrent method and inspect its IsInAsyncPostBack property 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                

            }

        }

    }


Based on this information we can determine the type of the Postback that has occurred on the page and apply our custom page logic accordingly.