在回发时保持动态添加的用户控件的状态?

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.

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

本站部分内容来源互联网,如果有图片或者内容侵犯您的权益请联系我们删除!

相关文档推荐

DispatcherQueue null when trying to update Ui property in ViewModel(尝试更新ViewModel中的Ui属性时DispatcherQueue为空)
Drawing over all windows on multiple monitors(在多个监视器上绘制所有窗口)
Programmatically show the desktop(以编程方式显示桌面)
c# Generic Setlt;Tgt; implementation to access objects by type(按类型访问对象的C#泛型集实现)
InvalidOperationException When using Context Injection in ASP.Net Core(在ASP.NET核心中使用上下文注入时发生InvalidOperationException)
LINQ many-to-many relationship, how to write a correct WHERE clause?(LINQ多对多关系,如何写一个正确的WHERE子句?)