Sunday, September 13, 2009

Detect asynchronous postback in ASP.NET

Everyone need to know about this. When no Ajax implementation in ASP.NET [older versions] asp.net developers know about only one post back. That is full page postback. We have a property to detect it in c# code with Page.IsPostBack. But, day to day technology is growing and we need to know/follow everything up to date. This is not new one or I didn't find it just recent. All these posts are my experience and part of sharing information.

Now a days everyone knows what is Ajax and how it will work. Everything is Asynchronous calls. So, in developing Ajax integrated applications in ASP.NET we need to know which is page full post back and which is asynchronous partial post back. So, This post will help you to understand it well.

As we know, ASP.NET Page Object has a property named IsPostBack, ScriptManager class also has a property named IsInAsyncPostBack and which returns true if page raised the async partial postback event.

Now, take a look at the usage of both.

//Checking for page postback
if (IsPostBack)
{
if (ScriptManager.GetCurrent(Page).IsInAsyncPostBack)
{
//Aynchronous postback raised.
}
else
{
//Normal page full postback.
}
}

Remember, Here whatever postback raised like full page postback or async partial postback IsPostBack set to true in both cases.

Hope you understood well about the both postback types and how to know which one raised. Please add your valuable comments.

2 comments:

  1. its very nice explanation about post back scenarions..thanks praveen

    ReplyDelete
  2. just one thing: place the code after the controls are initialized (not within the Page_PreInit event)

    ReplyDelete