Showing posts with label firing. Show all posts
Showing posts with label firing. Show all posts

Monday, March 26, 2012

window.onload event handler not firing on web page (AJAX BETA 2)

Any help on this one would be greatly appreciated. I'm new to Ajax and hopefully this will be a simple fix - i'm probably missing something obvious.

I have a simple website with a master page : SiteMaster.master. Inside here I've referenced the ScriptManager component
I've a simple content page which references the ScriptManagerProxy to include its own custom javascript - note no event handlers or listeners in here

The master page contains a number of controls that need to be resized immediately after the page loads and also when the page resizes

<asp:ScriptManagerID="smDefaultMaster"runat="server"EnablePartialRendering="true"ScriptMode="Release">
<Scripts>
<asp:ScriptReferencePath="~/Scripts/siteMaster.js"/>
</Scripts>
</asp:ScriptManager>


In the siteMaster.js I have 2 functions defined and I add handlers for these as per the new Sys.Dom.UI.AddHandler Methods in Beta 2. For some reason the pageLoaded event is never raised. The resize event works fine. Am i doing things correctly or am i messing something up.

function

pageLoaded () {
alert ("Load");
}

function pageLoaded () {
alert ("Resize");
}

$addHandler (window,'load', pageLoaded);
$addHandler (window,'resize', pageResized);

In the docs onthis page, you can read this:

During ordinary page processing in the browser, thewindow.onload DOM event is raised when the page loads initially. Similarly, thewindow.onunload DOM event is raised when the page is refreshed or when the user navigates away from the page.

However,these events are not raised during asynchronous postbacks. To help youmanage these types of events for asynchronous postbacks, thePageRequestManager manages a set of events that are similar towindow.load and other DOM events, but that also occur for asynchronous postbacks. For each asynchronous postback, all page events in thePageRequestManager class are raised and any attached event handlers are called.

Maybe it does help you.

Greets,
Nils Gruson

window.onload() not firing.

Hi,

The window.onload() is firing only once when the page is loaded for the first time. it is not fired when some event occurs in that page. I want to make it fire everytime asynchronous postback occurs. What is the solution?

Thanks.

Habeeb.

Use theclient-side pageLoad() function.

Or for a slightly more elegant solution:

<asp:ScriptManagerID="ScriptManager1"runat="server"/>
<scriptlanguage="javascript"type="text/javascript">
var prm = Sys.WebForms.PageRequestManager.getInstance();
prm.add_pageLoaded(pageLoadedFunc);

function pageLoadedFunc(sender, eventArgs) {
// Do whatever.
}
</script>

The script must be placed after the scriptmanager on the page. This will be called for any synchronous and asynchronous requests.


I'm curious why you'd consider that more elegant than simply:

function pageLoad() { // Do whatever.}


More elegant. I didn't say simpler. I'm just trying to lead the poster into more advanced concepts, so that maybe when the need arises, they'll say "Oh yeah, I remember that I can use the PageRequestManager to manage partial page updates and access the various events in the page lifecycle from client-side script."

Po-tay-to, po-tah-to. Didn't mean to sound disparaging in the first post. Smile


I didn't take it as disparaging. I was just curious why you thought that.

It's important to note that they aren't the same things. PageRequestManager.PageLoaded won't fire on the initial load, which the original poster seems to desire. However, Application.Load (pageLoad) does.


Actually, pageLoaded is the only event of the PageRequestManager that is raised by both asynchronousand synchronous postbacks. Therefore, it will fire on the initial load.


You're right. I wonder what the reasoning is behind duplicating that functionality.


The Application class is designed to expose events in the page life-cycle, and by chance the Application.Load event fires after an asychronous postback, but it was not "intended" to do so.

Whereas, the PageRequestManager class was designed to handle page life-cycle events in the presence of partial-page updating and asynchronous postbacks. That's why it is a little more appropriate in an AJAX methodology--that's what it was created to do. So it's a matter of doing it a way that happens to work versus doing it the way it was intended to work.

And the reason that the pageLoaded event fires after a synchronous postback is that it tracks any panels that were created, not just those that were modified (during an asynch postback). This allows you to do manipulation, animation, etc., on the initial load.

And this is probably way more information thanhabeebuddin ever wanted... Big Smile