Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to check if page is postback within reserved function pageLoad on ASP.NET AJAX

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

like image 318
Ricky Supit Avatar asked Sep 02 '08 23:09

Ricky Supit


4 Answers

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.

like image 150
Dave Ward Avatar answered Nov 15 '22 10:11

Dave Ward


@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).

like image 23
Ricky Supit Avatar answered Nov 15 '22 10:11

Ricky Supit


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.

like image 30
Darren Kopp Avatar answered Nov 15 '22 09:11

Darren Kopp


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.

like image 2
Greg Hurlman Avatar answered Nov 15 '22 08:11

Greg Hurlman