Showing posts with label onload. Show all posts
Showing posts with label onload. Show all posts

Monday, March 26, 2012

Window.OnLoad and callback problem

Hi,

I have a Javascript function that fires every time the page loads: triggered by the event window.onload...

Now i use an updatepanel and i need that function to be fired every time the callback occurs...

Wich is the event i should use?

Is there something like window.oncallback ?

Thanks in advance.

Antonio Li Causi.

Well, no. First, if you're using the ASP.Net ajax framework anyway, you're better off writing a function called pageLoad; the framework looks for it and fires it once everythign's done and ready to go on the first load. It's better than window.onload for this b/c it's coordinated w/ everything else the framework is trying to do for you.

In that pageLoad() method, you want to enter the line: Sys.WebForms.PageRequestManager.getInstance().add_pageLoaded(pageLoadedHandler) where pageLoadedHandler is the name of the function you want to fire when the page loads after async callback.

Example is here:http://ajax.asp.net/docs/ClientReference/Sys.WebForms/PageRequestManagerClass/PageRequestManagerPageLoadedEvent.aspx


Check this:ASP.NET AJAX Client Life-Cycle Events

and especiallypageLoaded Event

window.onload alternative

I believe there is a method in the Microsoft AJAX framework that is part of the client side (javascript). That allows multiple onload events to be registered easily.

I can't find it in the documentation though. Can someone point me in the right direction?

Thanks

You can use Sys.Application to register/deregister load and unload handlers.

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