问题描述
我有一个GridView
,其中有一些BoundFields
和两个TemplateFields
.在这两个TemplateFields
中,我动态创建了一个UserControls
,其中包含一个DropDownList
和一个TextBox
,用户可以对其进行修改.
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.
当我尝试在PostBack
之后获取控件的值时,BoundFields
中的值仍然存在,但是我的动态控件消失了.我可以再次创建它们,但无法获取用户的值...如何在丢失这些值之前获取这些值?
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?
这是我的一些代码:
在RowDataBound
事件中:
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.
推荐答案
您应在而不是,因为此事件在每次回发时都会触发,而RowDataBound
仅在GridView
被绑定到DataSource
的数据时才触发.
You should create dynamic controls in RowCreated instead of RowDataBound because this event gets fired on every postback whereas RowDataBound
only will fire when the GridView
gets databound to it's DataSource
.
必须在每个回发中使用与以前相同的ID重新创建动态创建的控件,然后将它们的值保留在,事件将正确触发(通过DropDownList的SelectedIndexChanged
事件).
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).
因此,您应该在RowCreated
中创建它们,并在RowDataBound
中填充"它们(例如DropDownList
数据源/项目或TextBox
-文本).
So you should create them in RowCreated
and "fill" them in RowDataBound
(f.e. the DropDownList
datasource/Items or a TextBox
-Text).
这篇关于回发后动态列消失的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!