动态列在回发后消失

发布于 2024-12-08 01:25:41 字数 1312 浏览 7 评论 0原文

我有一个带有一些 BoundFields 和两个 TemplateFields 的 GridView。在这两个 TemplateFields 中,我动态创建了 UserControls,其中包含用户可以修改的 DropDownListTextBox

当我尝试在 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

此刻的回忆 2024-12-15 01:25:41

您应该在 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 the GridView gets databound to it's DataSource.

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 in RowDataBound(f.e. the DropDownList datasource/Items or a TextBox-Text).

素年丶 2024-12-15 01:25:41

我一直在

EnableViewState="false"

GridView 属性中使用:。删除它解决了我的问题!

I had been using:

EnableViewState="false"

in the GridView attributes. Removing it solved my problem!

剪不断理还乱 2024-12-15 01:25:41

我刚刚做了

protected void Page_Load(object sender, EventArgs e)
{
    if (!(Page.IsPostBack))
    {
        // Put the selected items which u want to keep on postback
    }
    else
    {
        //regenerate auto created controls
    }
}

,效果也很好

I just did

protected void Page_Load(object sender, EventArgs e)
{
    if (!(Page.IsPostBack))
    {
        // Put the selected items which u want to keep on postback
    }
    else
    {
        //regenerate auto created controls
    }
}

and it worked as well

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文