简体   繁体   English

动态添加的UserControl在我点击它时消失了

[英]Dynamically Added UserControl disappears when I click on it

I have controls inside a UpdatePanel like this 我在UpdatePanel中有这样的控件

<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>

When an Item is selected, it will add a new UserControl to WidgetAdminControls WidgetAdminControls.Controls.Add(widget); 选择一个Item后,它会将一个新的UserControl添加到WidgetAdminControls WidgetAdminControls.Controls.Add(widget);

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. 但是,当我点击控件是disappers时,我假设因为页面被重建或者某些内容并且它不知道动态添加的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. 每种方法都有其优点/缺点。

Stateful 有状态

Most likely a stateful page will use Session; 很可能有状态页面将使用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). 必须注意不要将太多数据转储到Session中并正确清理(我通常使用管理器来包装Session并管理用户可以同时拥有多少个活动对象)。 The upside is that Session (even out of process) is very fast and secure. 好处是会话(甚至是在流程外)是非常快速和安全的。

Roundtrip 往返

This approach uses ViewState, hidden fields, or information in the URL. 此方法使用ViewState,隐藏字段或URL中的信息。 If information is included with the page, it should be measured in bytes , ie 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. 好处是你真的不需要做太多工作,并且没有清理所需的处理过程。

Database 数据库

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. 或者,您可以在每次单击时将更改保留到数据库并从数据库重建树,但我通常不会喜欢这种方法,除非单击代表一个非常重要的事件并且数据已完成并经过验证。

Very Simple Example 很简单的例子

Here is an example showing the use of Session to maintain a control tree. 这是一个示例,显示使用Session来维护控制树。 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
}

Dynamically added controls must be instantiated and initialized on each postback. 必须在每次回发时实例化并初始化动态添加的控件。 So in this case you have to load that control in page_init/load event. 因此,在这种情况下,您必须在page_init / load事件中加载该控件。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM