1

I have an ASP.NET UpdatePanel Triggered to refresh based on the Tick event of a Timer.

The page parses a custom XML file and inserts UserControls (containing GridViews) on the fly. This is executed from the Page_Load method.

if(!Page.IsPostBack)
  parseXmlFile();

The first time the page loads, all the controls are displayed as expected. The problem is that the first time the timer Tick event is fired, all the UserControls disappear.

In the Tick event I call a custom method RefreshData() which in turn rebinds the data to the GridView via an SqlDataSource.

   public void RefreshData() {
       GridView1.DataBind();
   ...

Maybe I don't understand how AJAX works in ASP.NET.

I only want to parse the XML file once (which creates all the UserControls in the page). From this point I just want to refresh the data contained in the GridView and change the Visibility state of the GridView.

jax
  • 37,735
  • 57
  • 182
  • 278

1 Answers1

1

This point is the one that you must focus on.

if(!PostBack)
  parseXmlFile();

You create the controls only the first time, so the controls on post back, when the Tick fires, the controls are not shown again.

Two solutions for that.
1. To parse again the XmlFile
2. To save all the controls on the viewstate, so on post back the controls are re-create from the viewstate.

I prefer to keep the xmlfile and just recreate them exactly as the first time.

If you wonder why the controls are not exist again, because they are not written anywhere on the data side of the page, just rendered on the html part, but the html part is un-known from code behind, only the data and the post data like the view state can recognize by code behind. So on post back the code behind must render again the page, but this control is now again unknown for the code behind and need to recreate them, ether by the same function that create them in first place, ether by get the saved of them on viewstate.

Aristos
  • 66,005
  • 16
  • 114
  • 150
  • In one of my UserControls I create a `Timer` that rotates between a number of different GridViews based on the Tick event. If I recreate from the XML each time, won't the Timer be reset every time also? – jax Jun 06 '12 at 12:33
  • @jax maybe, I am not sure, but you can set a variable on viewstate and known what is the next rotation id. If the timer is running on the update panel, when you update it, the timer has been left from previus, and also the javasript is not run again just because you place it with ajax, you need special code to run the javascript after the ajax update. What I mean is that you need to test it out and see how is response. I think timer stay as it is. – Aristos Jun 06 '12 at 12:35
  • @jax the ajax update of the content of the updatepanel is not also runs new javascript by default. This must be done with javascript commands. So when you update the updatepanel, only the content is update (not the javascript) Now if the dom change the previus javascript may fail to continue runs and stop. – Aristos Jun 06 '12 at 12:38
  • @jax see also http://stackoverflow.com/questions/3341623/asp-net-updatepanel-in-gridview-jquery-datepicker/3341741#3341741 and http://stackoverflow.com/questions/3257973/how-do-you-get-client-side-script-to-execute-on-an-asp-net-postback-from-an-up/3258167#3258167 – Aristos Jun 06 '12 at 12:40