本文介绍了回发后动态列消失的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个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).

这篇关于回发后动态列消失的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

06-23 23:12