添加基于 ViewState 的动态 ASP.NET 用户控件 - 但在 LoadViewState 阶段之前
设置:
我的页面有一个下拉列表,其值是动态填充的。根据选择的项目,在运行时动态创建许多文本框。然后,用户将信息填写到文本框中并单击提交按钮。
问题:
从提交按钮回发后,我需要在 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我建议直接使用 UniqueID 属性:
I'd suggest retrieving drop-down list value from the request directly using the UniqueID property: