动态列在回发后消失
我有一个带有一些 BoundFields
和两个 TemplateFields
的 GridView。在这两个 TemplateFields
中,我动态创建了 UserControls
,其中包含用户可以修改的 DropDownList
和 TextBox
。
当我尝试在 PostBack
之后获取控件的值时,BoundFields
中的值仍然存在,但我的动态控件消失了。我可以再次创建它们,但它不会获取用户的值...如何在这些值丢失之前获取它们?
这是我的一些代码:
在RowDataBound
事件中:
Select Case type
Case "BooleanBis"
e.Row.Cells(2).Controls.Clear()
Dim list1 As BooleanBisList = New BooleanBisList(avant, False)
e.Row.Cells(2).Controls.Add(list1)
e.Row.Cells(4).Controls.Clear()
Dim list2 As BooleanBisList = New BooleanBisList(apres, True)
e.Row.Cells(4).Controls.Add(list2)
Case "Boolean"
e.Row.Cells(2).Controls.Clear()
Dim list3 As BooleanList = New BooleanList(avant, False)
e.Row.Cells(2).Controls.Add(list3)
e.Row.Cells(4).Controls.Clear()
Dim list4 As BooleanList = New BooleanList(apres, True)
e.Row.Cells(4).Controls.Add(list4)
End Select
在我的按钮单击事件中,我尝试获取用户控件:
Case "String"
temp.ChampValeurApres = DirectCast(Tableau1.Rows(i).Cells(selectedColumn).Controls(1), TextBox).Text
但我收到它不存在的错误。
I have a GridView
with some BoundFields
and two TemplateFields
. In these two TemplateFields
, I dynamically create UserControls
containing a DropDownList
and a TextBox
, which users can modify.
When I try to get the values of the controls after a PostBack
, the values in BoundFields
are still there but my dynamic controls disappears. I can create them again but it won't get the user's values... How can I get these values before they're lost?
Here's some of my code:
In the RowDataBound
event:
Select Case type
Case "BooleanBis"
e.Row.Cells(2).Controls.Clear()
Dim list1 As BooleanBisList = New BooleanBisList(avant, False)
e.Row.Cells(2).Controls.Add(list1)
e.Row.Cells(4).Controls.Clear()
Dim list2 As BooleanBisList = New BooleanBisList(apres, True)
e.Row.Cells(4).Controls.Add(list2)
Case "Boolean"
e.Row.Cells(2).Controls.Clear()
Dim list3 As BooleanList = New BooleanList(avant, False)
e.Row.Cells(2).Controls.Add(list3)
e.Row.Cells(4).Controls.Clear()
Dim list4 As BooleanList = New BooleanList(apres, True)
e.Row.Cells(4).Controls.Add(list4)
End Select
In my button click event, I try to get the user control :
Case "String"
temp.ChampValeurApres = DirectCast(Tableau1.Rows(i).Cells(selectedColumn).Controls(1), TextBox).Text
but i get the error that it doesn't exist.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您应该在 RowCreated 中创建动态控件,而不是 < a href="http://msdn.microsoft.com/en-us/library/system.web.ui.webcontrols.gridview.rowdatabound.aspx">RowDataBound 因为此事件在每次回发时都会触发,而仅当
GridView
获取与其DataSource
的数据绑定时,RowDataBound
才会触发。动态创建的控件必须在每次回发时使用与以前相同的 ID 重新创建,然后它们在 ViewState 中保留其值 并且事件将正确触发(例如 DropDownList 的
SelectedIndexChanged
事件)。因此,您应该在
RowCreated
中创建它们,并在RowDataBound
中“填充”它们(例如DropDownList
数据源/Items 或TextBox-文本)。
You should create dynamic controls in RowCreated instead of RowDataBound because this event gets fired on every postback whereas
RowDataBound
only will fire when theGridView
gets databound to it'sDataSource
.Dynamically created controls must be recreated on every postback with the same ID as before, then they retain their values in the ViewState and events will fire correctly(f.e. a DropDownList's
SelectedIndexChanged
event).So you should create them in
RowCreated
and "fill" them inRowDataBound
(f.e. theDropDownList
datasource/Items or aTextBox
-Text).我一直在
GridView 属性中使用:。删除它解决了我的问题!
I had been using:
in the
GridView
attributes. Removing it solved my problem!我刚刚做了
,效果也很好
I just did
and it worked as well