2

So I have an ASP.NET page with two controls:

  • a GridView which displays rows from a SqlDataSource, and in which the user can select a row;
  • a DetailsView in which the user can see and edit the values of the selected row.

The DetailsView can update the underlying data, but even though the page reloads, the GridView still displays the old data until I manually reload the page in the browser.

How do I ensure that the GridView displays the correct data after the update in the DetailsView?

P.S. It may be important to note that due to reasons outlined in this other question, I had to use a custom OnItemUpdating event with the DetailsView. This event performs the SQL update command (successfully), sets the DetailsView back to ReadOnly mode (out of Edit mode) and then cancels the event (e.Cancel = true). If this event canceling also somehow cancels the GridView updating, how do I manually tell it to update itself?

P.P.S.: I discovered this similar question, but the answer doesn’t work: it resets the entire page back to pristine state, which means the GridView loses its selected row and the DetailsView disappears. I don’t want that.

Community
  • 1
  • 1
Timwi
  • 65,159
  • 33
  • 165
  • 230

2 Answers2

3

on page load:

YourGridViewID.DataBind()
boruchsiper
  • 2,016
  • 9
  • 29
  • 53
  • Perfect! Thanks! I added it to the end of the `DataView`’s `OnItemUpdating` handler instead of `Page_Load()` though. – Timwi Feb 01 '12 at 09:17
0

If your OnItemUpdating event generates postback

protected void Page_Load(object sender, EventArgs e)
{
    if (IsPostBack)
    {
        GridView.DataBind();//rebinding it with considering changes DetailView
    }
}

If it doesn't work let me know.

rofans91
  • 2,970
  • 11
  • 45
  • 60