Saturday, March 24, 2012

Ajax Timer is causing Page_Load every time that it fires.

Hi,

I have an Ajax Timer on my page, which fires every 3 seconds. When it fires, if approriate, it will bind a datatable to a gridview (which is inside an update panel) and change the text of a label (which is in a different update panel); otherwise it will do nothing.

The timer works and completes its tasks correctly but everytime it fires it also causes the Page_Load method to fire. This is causing problems because the formatting of the gridview is then changed.

I hope someone is able to help!

Thanks

Matt

This is just how it is. When the timer event is raised, it will cause Page_Load to fire, then the Timer_Tick event, similar to how events typically work in a standard ASP.Net page postback. The difference is that the request is being delivered asynchronously to the server via XML and javascript.

If you have code in Page_Load that you don't want executing every time, look to using IsPostback to limit them to executing only on the initial Page_Load.


like mentioned above, thats how it works, and checking the page_load function to see if you posted back is a good way to help fix this. Try code like this

if(!IsPostBack){//Put all the code here that you want to run only on //the first time the page loads}//Everything outside of that if will run everytime there is a postback

Hi, thanks for that.

I have actually used the "!IsPostBack" statement to limit what code fires. But even when there is nothing with in the Page_Load method the GridView (in the update panel) is still being reloaded.


is the update panel set to always update, if so try putting it to conditional


keyboardcowboy:

is the update panel set to always update, if so try putting it to conditional

No, that doesn't help.

I think that the Gridview is being rendered (not sure if this is the correct term) at Page_Load.

Is there any way to limit when a gridview can be rendered?


I think that if the gridview and the timer are in the same updatepanel, then the gridview will re-render. If this is the case for you, then try moving the timer into a separate updatepanel and ensuring that UpdateMode="Conditional" is set for the gridview updatepanel.

Jason


Hi,

As a matter of fact, a partial request and a normal request will both make the requested page experiences a normal life cycle. So, page_load method will be fired in both cases.

You can distinguish it via ScriptManager1.IsInAsyncPostBack property.

Hope this helps.

No comments:

Post a Comment