Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations TouchToneTommy on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Datagrid not refreshing after postback

Status
Not open for further replies.

mirirom

Programmer
Jul 21, 2001
110
US
hi,

i have a DataGrid that's bound to a table within an untyped DataSet. during the application's use, the table has rows added, updated, deleted, etc which is handled by a method in the page's code behind.

after the method executes the page reloads. my Page_Load handler explicitely states that it should be rebuilding the DataGrid regardless of the page being a postback or not since the display information in the datagrid isn't part of the viewstate. fine.

now, if i *refresh* the page (F5), the datagrid shows correctly. i've checked the dataset and it's table to see if there's any difference in content for after method execution and refresh: they're identical. i've also verified that my methods that rebuild the DataGrid are called after the data has been changed.

so that being said, why is the new content being ignored?
should i register a StartUpScript to force the form to submit? (i tested this and this does work, but there's gotta be a better way than making two trips to the server for a refresh).

for what it's worth, the datagrid is built with nearly all custom template columns.

toy code below

Code:
void Page_Load(object sender, System.EventArgs e){
  if(!Page.isPostBack){
     /* build the initial dataset & save it to a Session
      * var for later use... */
  }
  buildDataGrid();
}

void buildDataGrid(){
  //build & populate the datagrid
}

//called by a control in DataGrid, e.g. btn, ddl, cbx, etc
void aDataGridEvtHander(object sender, System.EventArgs e){
  /* update a table in the dataset & save DataSet back to
   * the Session var */
}

any thoughts? thanks in advance...

..:: mirirom ::..
 
The way that the page executes, I've found, is not as you would assume:

- Click button
- Data is executed on
- Page is re-loaded

It actually goes this way:

- Click button
- Page is re-loaded
- Data is executed on

So, instead of binding the grid every time the page loads (which will cause you always to be one step behind on the displayed data), re-bind the grid every time you execute any changes to the data. Maybe something like below:

Code:
void Page_Load(object sender, System.EventArgs e){
  if(!Page.isPostBack){
     /* build the initial dataset & save it to a Session
      * var for later use... */
     buildDataGrid();
  }
}

void buildDataGrid(){
  //build & populate the datagrid
}

//called by a control in DataGrid, e.g. btn, ddl, cbx, etc
void aDataGridEvtHander(object sender, System.EventArgs e){
  /* update a table in the dataset & save DataSet back to
   * the Session var */
}

void btn_Click(Object sender, EventArgs e)
{
   // Add/Delete functionality here
   buildDataGrid();
}

-----------------------------------------------
"The night sky over the planet Krikkit is the least interesting sight in the entire universe."
-Hitch Hiker's Guide To The Galaxy
 
hey atomic, thx for the reply.

yeah, in one of my tests, i have the event handler make an explicit call to the building method.

it's interesting that when stepping through the code *starting* from the event hander for the control in the datagrid...

~ the table in the dataset is updated. the values can be
read in the "immediate" window.
~ the load event is fired (as shown in the stack trace)
~ the Page_Load event is executed and all building methods
are called. the dataset, its tables & values are
available in the "immediate" window from the perspective
of the DataGrid object, meaning the DataGrid has
successfully *bound* to the updated dataset *before*
the Page_Load method has finished executing
~ the preRender event is fired (shows in stack trace)
~ the render event is fired (in stack trace)
~ html is [supposedly] shipped to the client

from this it seems that there's an issued with the client cache.

i've only been able to replicate this behavior for datagrids and datalists. all other standard controls seem to bind correctly and render appropraitely.

weird...

..:: mirirom ::..
 
hey again Atomic,

i totally stand corrected; was reading the trace wrong! your're completely right about the order of ops here where Page_Load is being called prior to the handler from the DataGrid.

thanks! i've added a myGrid.DataBind() method in the control's event handler to refresh before rendering.

..:: mirirom ::..
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top