在回发时保持动态添加的用户控件的状态? [英] Maintain the state of dynamically added user control on postback?

查看:17
本文介绍了在回发时保持动态添加的用户控件的状态?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个只包含一个文本框的用户控件,在另一个表单上我动态添加了这个用户控件,用户可以多次添加用户控件.我使用会话变量来重新创建用户控件(也许这种方法听起来不太酷).重新创建控件后,文本框的值显然消失了.是否有任何解决方案可以在回发时保持用户控件的状态?

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.

这篇关于在回发时保持动态添加的用户控件的状态?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆