views:

6356

answers:

9

I'm looking for a way to check within pageLoad() if this method is raised during load event because of a postback/async postback or because of being loaded and access the first time.

This is similar to Page.IsPostback property within code behind page.

TIA, Ricky

+2  A: 

You could have a hidden input that you set to a known value on the server side if it's a postback/callback - and your javascript could check that value.

That said, I really hope that there's a client-only solution for this.

Edit: @mmattax - I believe he's looking for a client-side solution - the JavaScript equivalent of that.

Greg Hurlman
+2  A: 

What you can do is wire up to the load event of the Sys.Application class. you can then use the isPartialLoad property of the Sys.ApplicationLoadEventArgs class. I believe that would let you know if you are in a async postback or not.

To know if you are in a post back, you'll have to handle that in server side code and emit that to the client.

Darren Kopp
A: 

You can still use Page.IsPostback during an async call.

Shawn Simon
+1  A: 

@Darren: Thanks for the answer. I had tried to create pageLoad with event argument ApplicationLoadEventArgs as parameter (see below). However according to this:

The load event is raised for all postbacks to the server, which includes asynchronous postbacks.

As you have indicated, the isPartialLoad property does not cover all postback scenarios. It'll be nice if the event argument also contain isPostback property.

   function pageLoad(sender, arg) {
      if (!arg.get_isPartialLoad()) {
          //code to be executed only on the first load
      }
   }

@mmattax: I'm looking for property that can be called from client-side (javascript).

Ricky Supit
A: 

Application.Init is probably a more appropriate event to use, if you only want the code to execute on the first load.

Dave Ward
A: 

@Dave Ward: This normally would work. However, the code is to attach event on behavior object. Because the creation of behavior object happens during Application.Init, attaching to that event will lead to unpredictable behavior.

It will be nice if there is PostInit event.

Ricky Supit
+1  A: 

One way you could do that is to wire up an Application.Load handler in Application.Init, then have that handler unbind itself after running:

Sys.Application.add_init(AppInit);

function AppInit() {
  Sys.Application.add_load(RunOnce);
}

function RunOnce() {
  // This will only happen once per GET request to the page.

  Sys.Application.remove_load(RunOnce);
}

That will execute after Application.Init. It should be the last thing before pageLoad is called.

Dave Ward
A: 

@Dave Ward: The use of RunOnce method works perfectly. This solve my problem without having the workaround to check first if handler already exist before attaching to an event.

I'll mark your answer as an accepted answer. Thanks again.

Ricky Supit
A: 

Here's our Ajax equivalent to isPostback which we've been using for a while.

public static bool isAjaxRequest(System.Web.HttpRequest request)
 {//Checks to see if the request is an Ajax request
  if (request.ServerVariables["HTTP_X_MICROSOFTAJAX"] != null ||
   request.Form["__CALLBACKID"] != null)
   return true;
  else
   return false;
 }
Compulsion