问题描述
我有一个 GridView控件
一些绑定列
和两个模板列
。在这两个模板列
,我动态地创建用户控件
A 的DropDownList
和文本框
,用户可以修改。
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?
下面是我的一些code的:
Here's some of my code :
在的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. Can someone give me a hint please?
推荐答案
您应该创建RowCreated而不是RowDataBound因为本次活动得到每个回发解雇,而的RowDataBound 只会当 GridView控件
被数据绑定到它的火力数据源
。
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每个回发作为之前重新创建,那么他们保留其值在的以及事件将正确触发(FE一个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
补他们(FE的的DropDownList
数据源/项或文本框
- 文本)。
So you should create them in RowCreated
and "fill" them in RowDataBound
(f.e. the DropDownList
datasource/Items or a TextBox
-Text).
这篇关于动态列回发后自败的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!