Persisting GridView data across PostBacks?
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 theList(Of Person)
fromSession()
- Any events fire, and modify the
List(Of Person)
. - After any event, the
List(Of Person)
gets saved back intoSession()
, 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 ac开发者_开发知识库ross postbacks?
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.
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.
精彩评论