在回传中持久化GridView数据? [英] Persisting GridView data across PostBacks?
问题描述
好的,这是我的基本ASP.NET页面设置:我有一个带有GridView的页面,其中包含ContentTemplates.您可以随时添加行和编辑/删除行.有一个新建"按钮可以创建新行.
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.
所有这些数据都绑定到自定义数据对象.因此,如果我有一个"People"的GridView,并且每一行都有"FirstName","LastName"和"Gender"(带有TextBox和DropDown控件),那么我将拥有一个"Person"对象,该对象具有"FirstName"的公共属性. ,"LastName"等.我已经正确设置了绑定,并且可以将数据从对象推送到GridView中,并且可以使用Session变量保留对象.我的页面生命周期结构如下所示:
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
:从Session()
加载 - 任何事件都会触发,并修改
List(Of Person)
. - 发生任何事件后,
List(Of Person)
都将保存回到Session()
中,然后被DataBound绑定到GridView(随后的任何字段也都是DataBound,例如DropDownList.
List(Of Person)
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.
我的问题是:每当我在GridView中填写行,然后添加新行(无论如何都不会保存数据库)时,我的字段就会清除,并且不会在PostBacks中持续存在.那么,如何通过回发之间的数据绑定来保留我的自定义数据对象呢?
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?
推荐答案
自己弄清楚了.我只需要处理TextChanged
,SelectedIndexChanged
等事件,然后将新数据保存到我的自定义对象中.现在我想到了,就说得通了.
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.
感谢大家的帮助.
这篇关于在回传中持久化GridView数据?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!