GridView not Rebinding Properly After Postback

ぐ巨炮叔叔 提交于 2019-11-29 04:23:30

Gridviews are not re-bound on postback, their rows are pulled back from viewstate. Resetting the gridview's DatasourceID to the object data source ID on page load (or init?) will cause the gridview to be rebound.

Dumb idea, but have you checked the page load event with the if(!Page.IsPostBack)?

From ASP.NET Page Framework Overview :

Page_Load: During this event, you can perform a series of actions to either create your ASP.NET page for the first time or respond to client-side events that result from a post. The page and control view state have been restored prior to this event. Use the IsPostBack page property to check whether this is the first time that the page is being processed. If it is the first time, perform data binding. Also, read and update control properties.

Where as

Page_PreRender: The PreRender event is fired just before the view state is saved and the controls are rendered. You can use this event to perform any last minute operations on your controls.

In effect

Because the page framework is a stateless a and disconnected model, every time a client requests an .aspx page, many things occur during the page processing ...

So in effect, you could be doing your checking before the viewstate is being set rather than after the viewstate has been restored. The most common place to check for if(!Page.IsPostBack) is typically in the Page_Load event.

Your example shows

  TestGridView.Columns.RemoveAt(0); 

but did you really mean

  TestGridView.Rows.RemoveAt(0); 

(and is this the problem?)

I had a similar problem with dynamically binding a TreeView to an XmlDataSource which changed the xml source on every postback. Setting EnableCache to false fixed it. Have you tried this? (Consider the Linq2sql object already caches, if your IQueryable is using a Linq2sql object, that is)

<asp:ObjectDataSource ID="TestDataSource" runat="server" EnableCaching="false"
    EnablePaging="true" SelectCountMethod="GetDetailCount" 
    SelectMethod="GetDetails" TypeName="MyApp.PageClass" />

if that doesn't work, try this coupled with the above:

protected override void OnPreRender(EventArgs e)
{
   base.OnPreRender(e);
   BindData();
}

After looking at the code behind a bit more, I stumbled across page property values being stored in ViewState. Once I changed it over to Session, they work.

I had a similar situation in which the updated values of a row didn't show no matter how I tried to databind after updating.

The GridView was bound to an ObjectDataSource, and the problem occurred after I switched its backing object from a DataSet to an Entity Framework query

Enabling ViewState for the GridView did the trick for me, thus:

<asp:GridView ID="GridViewTransporters" PageSize="100"
runat="server" AllowPaging="True" AllowSorting="True"
AutoGenerateColumns="False" DataSourceID="ObjectDataSourceTransporters"
DataKeyNames="Id" EnableViewState="True">
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!