问题
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 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