添加基于 ViewState 的动态 ASP.NET 用户控件 - 但在 LoadViewState 阶段之前

发布于 2024-10-14 04:48:37 字数 815 浏览 2 评论 0原文

设置:

我的页面有一个下拉列表,其值是动态填充的。根据选择的项目,在运行时动态创建许多文本框。然后,用户将信息填写到文本框中并单击提交按钮。

问题:

从提交按钮回发后,我需要在 Page_Init 期间(在 LoadViewState 之前)再次动态创建文本框,以便在 ViewState 加载后,我的 Button_Click 事件可以保存/执行用户输入的任何操作。问题是,我无法根据下拉列表中的选择重新创建文本框,因为 LoadViewState 尚未“选择”下拉列表。

那么,我如何从视图状态读取,创建文本框,然后让视图状态填充文本框,然后 Button_Click 将使用这些值?

我尝试的一件事是重写 LoadViewState 函数,以便我可以从视图状态读取,创建框,然后再次加载视图状态。这不起作用,因为调试器似乎从未命中我覆盖的函数。

    Protected Overrides Sub LoadViewState(ByVal savedState As Object)

        MyBase.LoadViewState(savedState)

        //'Do something like add controls
        Dim test As String = RecordList.SelectedValue
        //'Create controls using value "Test"

        MyBase.LoadViewState(savedState)

    End Sub

任何帮助将不胜感激。如果需要,我可以发布更多代码。

谢谢,

大卫

Setup:

My page has a drop down whose values are dynamically populated. Based on which item is selected, a number of TextBoxes are dynamically created during runtime.The user then fills in information into the textboxes and clicks a submit button.

Problem:

After postback from the submit button, I need to again dynamically create the TextBoxes during Page_Init (BEFORE LoadViewState) so that after the ViewState loads, my Button_Click event can save/do whatever with the user input. PROBLEM is, I cannot recreate the textboxes based on the selection in the dropdown, because the dropdown hasn't been "selected" yet by LoadViewState.

SO, how can I read from the view state, create my textboxes, then let the viewstate populate the textboxes, and then the Button_Click will use the values??

The one thing I've attempted is to override the LoadViewState function so that I can read from the view state, create the boxes, and then load the viewstate again. This did NOT work, because the debugger never seemed to hit my overridden function.

    Protected Overrides Sub LoadViewState(ByVal savedState As Object)

        MyBase.LoadViewState(savedState)

        //'Do something like add controls
        Dim test As String = RecordList.SelectedValue
        //'Create controls using value "Test"

        MyBase.LoadViewState(savedState)

    End Sub

Any help would be appreciated. I can post more code if needed.

Thanks,

David

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(1

荒路情人 2024-10-21 04:48:37

我建议直接使用 UniqueID 属性:

Protected Sub Page_Init(ByVal sender As Object, ByVal e As EventArgs)
    Dim selectedValue As String = Request.Form(RecordList.UniqueID)
    ' Recreate your dynamic controls based on the selected value
End Sub

Protected Sub Button_Click(ByVal sender As Object, ByVal e As EventArgs)
    ' Examine dynamic controls and their values (retireved from the ViewState)
End Sub

I'd suggest retrieving drop-down list value from the request directly using the UniqueID property:

Protected Sub Page_Init(ByVal sender As Object, ByVal e As EventArgs)
    Dim selectedValue As String = Request.Form(RecordList.UniqueID)
    ' Recreate your dynamic controls based on the selected value
End Sub

Protected Sub Button_Click(ByVal sender As Object, ByVal e As EventArgs)
    ' Examine dynamic controls and their values (retireved from the ViewState)
End Sub
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文