Tuesday, August 07, 2007

ASP.NET Ajax comes with a nice way to attach to a load event of a page. To do it just call Sys.Application.add_load and pass it a function as an argument. Done. Everything works as expected... WRONG!

It comes out that add_load fires on each callback and not only when the page loads for the first time. That is of course a problem if you want something to happen on page load only.

From what I've been able to learn, this behavior is by design. Although it is never mentioned in a strigh forward way in a description:

"Raised after all scripts have been loaded and after the objects in the application have been created and initialized."

There are some places in the documentation, that suggests that this is a way it was intended to be. First of all, ASP.NET Ajax tries to mimic that ASP.NET Page life cycle and as such, fires the load event every time there is a callback. The other thing that makes me believe, that load's behavior is there by design is this:

"You can use the event arguments to determine whether the page is being refreshed as a result of a partial-page update and what components were created since the previous load event was raised."

So now I know, but I still cannot accept the naming convention. Sys.Application plainly suggests, it's a kind of singleton, and not a per load thingy.

Tuesday, August 07, 2007 3:33:32 PM (Central European Standard Time, UTC+01:00)
It only happens for UpdatePanel, when a Web Service callback occurs those events does not fire.
Friday, August 17, 2007 7:43:17 PM (Central European Standard Time, UTC+01:00)
This was a pain to me.

Here is the code I used to remove it from pageloads

//notify of loading of this script
if (typeof(Sys) !== 'undefined')
{
Sys.Application.notifyScriptLoaded();
//set up a handler for the application start
Sys.Application.add_load(ApplicationLoadHandler)
}

function ApplicationLoadHandler(sender, args)
{
if (args.get_isPartialLoad()) return;
//find out when new pages are loaded.
Sys.WebForms.PageRequestManager.getInstance().add_pageLoaded(pageLoaded);
}
Jack Bradham
Tuesday, March 18, 2008 4:18:23 PM (Central European Standard Time, UTC+01:00)
Thank you for this script. Was driving me nutch.

I had the following code:
Sys.Application.add_load(ApplicationLoadHandler);
function ApplicationLoadHandler(sender, args) {
Sys.WebForms.PageRequestManager.getInstance().add_beginRequest(beginRequest);
Sys.WebForms.PageRequestManager.getInstance().add_pageLoaded(pageLoaded);
}

And when i did the third callback pageLoaded was called 3 times.
Name
E-mail
Home page

Comment (Some html is allowed: a@href@title, strike) where the @ means "attribute." For example, you can use <a href="" title=""> or <blockquote cite="Scott">.  

Enter the code shown (prevents robots):

Live Comment Preview