GridView contents don’t update when underlying data changes

会有一股神秘感。 提交于 2019-12-20 03:20:06

问题


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.


回答1:


on page load:

YourGridViewID.DataBind()



回答2:


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.



来源:https://stackoverflow.com/questions/9093145/gridview-contents-don-t-update-when-underlying-data-changes

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!