如何将用户输入的数据从动态创建的控件保存到会话状态?
使用 C# (asp.net),
我以编程方式创建几个下拉列表(随机数)。 用户从每个列表中选择一个特定值,我需要保存用户选择的值。
如果我尝试使用按钮单击事件将数据保存到会话状态,则表示尚未创建下拉列表对象。 (显然因为我正在 !IsPostBack 下的页面加载事件中创建下拉列表。)
如果我尝试将数据保存到 IsPostBack 下的页面加载事件中的会话状态,我只能从每个列表中获取第一个值。 (显然,因为在回发后重新创建页面时,下拉列表已重新创建,并且用户输入的数据会丢失)。
单击按钮时如何保存用户从下拉列表中选择的值? 我在代码隐藏文件中执行此操作。
using c# (asp.net)
i'm programmatically creating several drop down lists (random number).
the user selects a particular value from each list and i need to save the user selected value.
if i try to save the data to session state by using a button click event it says the drop down list object hasn't been created. (obviously cuz i'm creating the drop down lists in the page load event under !IsPostBack.)
if i try to save the data to session state in the page load event under IsPostBack i only get the first value from each list. (obviously cuz when the page is recreated after the postback, the drop down lists have been recreated and the user entered data is lost).
How do i save the user selected value from the drop down lists when a button is clicked?
i'm doing this in the code behind file.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
正如 John Saunders 所说,您必须在每次回发时重新创建所有 DropDownList 控件。 此外,您必须在恢复状态之前创建它们。 这意味着页面加载事件中的“under !IsPostBack”已经太晚了。 将其移至 Page Init。
As John Saunders said, you have to recreate all of your DropDownList controls on every postback. Additionally, you have to create them before state is restored. That means that "under !IsPostBack" in the page load event is already too late. Move it up to Page Init.
您必须在每次回发时重新创建所有下拉菜单。
You have to recreate all of the dropdowns on every postback.
就我个人而言(无法切换到 MVC),我会考虑完全摆脱回发并直接从标准 HTTP POST 请求读取值。
使用回发的动态控制的实现通常会变得相当复杂且难以遵循
Personally (without the ability to switch to MVC) I would consider getting rid of the postbacks altogether and reading the values back directly from a standard HTTP POST request.
The implementation of dynamic controls with postbacks often ends up rather convoluted and difficult to follow