Monday, March 26, 2012

UpdatePanel + StartupScript + Sys.Timer

I'm using an UpdatePanel with a user control somewhere in there. The usercontrol registers a client startup script (ClientScript.RegisterStartupScript) in which a Sys.Timer object is created.

When the updatepanel refreshes via Atlas, the startup script gets executed again (which I believe is expected behavior), causing a new Timer to be created.

The old timer is still around though, causing the tick event handler code to be executed twice as often as required.

What would be the best way to get around this? My current solution has been to update the startup script code to skip timer creation if a timer already exists. Another solution I could think of would be to call dispose() on the old timer at that time.

Is there a way to register client script to execute when an asynchronous postback occurs? In case the resulting updatepanel contents no longer contains the usercontrol with timer, it would be wise to dispose the timer at such a time.

Greetings,

Wouter

hello.

i think that you have several options...for instance, you could use the ispostback property of the page (it'll be false on the 1st rendering; since you only want to add the timer one time, this could solve your problem)...


Luis Abreu:

i think that you have several options...for instance, you could use the ispostback property of the page (it'll be false on the 1st rendering; since you only want to add the timer one time, this could solve your problem)...

Not really sure about IsPostBack. The scenario where I encountered this problem involved webparts in an updatepanel, where the timer is created by one of the webparts. When starting with a blank page and adding the webpart with timer, IsPostBack will be true.

Also, when this webpart is removed from the page, my feeling is that the timer should be stopped.


hello again.

hum...then, you can probably do something like this:

1. add the timer through xml-script
2. on the clientregistermethod, start the time
3. when the web part is removed, inject client script to stop it

No comments:

Post a Comment