Maintain the state of dynamically added user control on postback?(在回发时保持动态添加的用户控件的状态?)
问题描述
我有一个只包含一个文本框的用户控件,在另一个表单上我动态添加了这个用户控件,用户可以多次添加用户控件.我使用会话变量来重新创建用户控件(也许这种方法听起来不太酷).重新创建控件后,文本框的值显然消失了.是否有任何解决方案可以在回发时保持用户控件的状态?
I have a user control that contains only a text box and on another form I add this user control dynamically, a user can add the user control many times. I use a session variable to recreate the user control (maybe this approach doesn't sound cool). After recreating the control the value of the textbox disappears, obviously. Is there any solution to maintain the state of the user control on postback?
推荐答案
我过去也遇到过同样的问题.
I've had the same problem in the past.
我所做的是给动态添加的控件一个 ID,并确保它在回发时也保留该 ID(在我的情况下,我将所有信息保留在会话中,然后重新创建了控件).
What I did was give the dynamically-added control an ID, and made sure it retained that ID also on postback (in my case, I kept all the information in the session, and re-created the controls).
一旦回发控件具有与以前相同的 ID,Microsoft 就会变魔术并使用回发前值重新填充控件.
Once the postbacked control has the same ID as as before, Microsoft did magic and refilled the controls with the pre-postback values.
这篇关于在回发时保持动态添加的用户控件的状态?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:在回发时保持动态添加的用户控件的状态?
基础教程推荐
- 为什么Flurl.Http DownloadFileAsync/Http客户端GetAsync需要 2022-09-30
- MS Visual Studio .NET 的替代品 2022-01-01
- 如何在 IDE 中获取 Xamarin Studio C# 输出? 2022-01-01
- 如何激活MC67中的红灯 2022-01-01
- c# Math.Sqrt 实现 2022-01-01
- SSE 浮点算术是否可重现? 2022-01-01
- 将 Office 安装到 Windows 容器 (servercore:ltsc2019) 失败,错误代码为 17002 2022-01-01
- 将 XML 转换为通用列表 2022-01-01
- rabbitmq 的 REST API 2022-01-01
- 有没有办法忽略 2GB 文件上传的 maxRequestLength 限制? 2022-01-01