Monday, March 26, 2012

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

No comments:

Post a Comment