问题描述
我在像这样的 UpdatePanel 中有控件
I have controls inside a UpdatePanel like this
<asp:ScriptManager ID="ScriptManager1" runat="server"></asp:ScriptManager>
<asp:UpdatePanel ID="UpdatePanel1" runat="server">
<ContentTemplate>
Step 1 (Choose a Widget to Manage)<br />
<asp:DropDownList ID="DropDownWidgetSelection" runat="server"
OnSelectedIndexChanged="DropDownWidgetSelection_SelectedIndexChanged"
AutoPostBack="True">
</asp:DropDownList>
<div id="WidgetAdminControls" runat="server"></div>
</ContentTemplate>
</asp:UpdatePanel>
当一个 Item 被选中时,它会添加一个新的 UserControl 到 WidgetAdminControlsWidgetAdminControls.Controls.Add(widget);
When an Item is selected, it will add a new UserControl to WidgetAdminControls
WidgetAdminControls.Controls.Add(widget);
然而,当我点击控件消失时,我假设是因为页面被重建或某些东西并且它不知道动态添加的UserControl.
However, when I click on the control is disappers, I assume because the page is rebuilt or somehting and it does not know about the dynamically added UserControl.
我该如何解决这个问题?
How do I fix this problem?
推荐答案
你是对的,页面是重建的,因此你必须在每次部分或完整回发时重新创建控制树.
You are correct, the page is rebuilt, thus you must recreate the control tree with every partial or complete postback.
要实现这一点,您必须使页面有状态或包含足够的信息以在每次往返时重新创建控制树.每种方法都有其优点/缺点.
To accomplish this, you must make your page stateful or include enough information to recreate the control tree with every roundtrip. Every approach has its advantages/disadvantages.
有状态
一个有状态的页面很可能会使用 Session;必须注意不要将太多数据转储到 Session 中并正确清理(我通常使用管理器来包装 Session 并管理用户一次可以拥有多少活动对象).好处是会话(即使在进程外)非常快速和安全.
Most likely a stateful page will use Session; care must be taken not to dump too much data into Session and to cleanup properly (I usually use a manager to wrap Session and manage how many active objects a user can have at once). The upside is that Session (even out of process) is very fast and secure.
往返
此方法使用 URL 中的 ViewState、隐藏字段或信息.如果页面中包含信息,则应以字节为单位进行测量,即它应该非常小.它也不能免于篡改.
This approach uses ViewState, hidden fields, or information in the URL. If information is included with the page, it should be measured in bytes, i.e. it should be extremely small. It is also not immune to tampering.
好处是你真的不需要做太多事情来完成这项工作,也不需要清理的处置过程.
The upside is that you really don't have to do much to make this work, and there is no disposition process needed to cleanup.
数据库
或者,您可以通过每次单击将更改保存到数据库并从数据库重建树,但我通常不喜欢这种方法,除非单击代表非常重要的事件并且数据是完整的并且验证.
Alternatively, you could persist your changes to a database with every click and rebuild the tree from the database, but I'm not usually not a fan of this approach unless the click represents a very significant event and the data is complete and validated.
非常简单的例子
这里有一个例子,展示了使用 Session 来维护控制树.这应该可以工作,但它不能解释很多事情,例如用户在同一会话中打开同一页面的另一个副本.管理控制树可能会变得非常复杂.
Here is an example showing the use of Session to maintain a control tree. This should work but it doesn't account for many things, such as the user opening another copy of the same page in the same session. Managing control trees can get quite complex.
private List<string> _listOfStrings
{
get
{
return (List<string>)Session["_listOfStrings"];
}
set
{
Session["values"] = value;
}
}
protected override OnInit( EventArgs e )
{
if( !Page.IsPostback )
{
_listOfStrings = new List<string>();
}
BuildControlTree();
}
private void BuildControlTree()
{
foreach( string s in _listOfStrings )
{
// add a control to the control tree
}
}
protected void btnAddItem_Click( object sender, EventArgs e )
{
_listOfStrings.Add( "some new item" );
// insert the control into the tree
}
这篇关于单击它时动态添加的 UserControl 消失的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!