Persisting GridView data across PostBacks?

只愿长相守 提交于 2019-12-12 05:39:12

问题


Alright, so here's my basic ASP.NET page setup: I've got a page with a GridView that has ContentTemplates in it. You can add a row and edit/remove rows at any time. There's a "New" button that creates a new row.

All of this data is bound to custom data objects. So if I have a GridView of "People" and each row has "FirstName", "LastName", and "Gender" (with TextBox and DropDown controls), I then have a "Person" object which has public properties for "FirstName", "LastName", etc. I have the binding set up correctly, and I can push data into the GridView from the object, and I persist the object with the Session variable. My page lifetime structure looks something like this:

  • Page_Load: Loads the List(Of Person) from Session()
  • Any events fire, and modify the List(Of Person).
  • After any event, the List(Of Person) gets saved back into Session(), and is then DataBound to the GridView (and any subsequent fields are also DataBound, such as the DropDownList.

My question is: Whenever I fill in rows in the GridView, and then add a new row (there is no database saving going on whatsoever), my fields clear out and don't persist across PostBacks. So, how can I persist my custom data objects with databinding across postbacks?


回答1:


Work with your custom data objects in the Pre_Init event.

Save your data the ViewState, not a session. This way you ensure you aren't loosing the session, hence your work.




回答2:


Figured it out myself. I just needed to handle the TextChanged, SelectedIndexChanged, etc. events and then save the new data into my custom objects there. Makes sense now that I think about it.

Thanks for everyone's help.



来源:https://stackoverflow.com/questions/5542500/persisting-gridview-data-across-postbacks

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