单击按钮回发后,UpdatePanel中的自定义usercontrol内容消失 [英] custom usercontrol content in UpdatePanel disappears on button click postback

查看:158
本文介绍了单击按钮回发后,UpdatePanel中的自定义usercontrol内容消失的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我具有以下功能,该功能通过在默认的.aspx占位符中添加/加载ascx自定义用户控件来更新UpdatePanel内容:

I have the following function that updates the UpdatePanel content by adding/loading an ascx custom usercontrol in the placeholder that is in default.aspx:

 protected void NavigationTab_Click(string ascxpath)
                {           
                        Control ctrl = LoadControl(ascxpath);
                        //cphmaincontent is my asp ContenPlaceHoderId in masterpage
                        PlaceHolder phmaincontent = (PlaceHolder)cphmaincontent.FindControl("phmaincontent");
                        phmaincontent.Controls.Clear();
                        phmaincontent.Controls.Add(ctrl);
                        upmaincontent.Update();            
                }

母版页更新面板:

<asp:UpdatePanel ID="upmaincontent" runat="server" UpdateMode="Conditional">
                    <ContentTemplate>
                        <asp:Label ID="lbmsg" runat="server" Text=""></asp:Label>
                        <asp:ContentPlaceHolder ID="cphmaincontent" runat="server">                       
                        </asp:ContentPlaceHolder>                         
                    </ContentTemplate>
                </asp:UpdatePanel>            

我从另一个自定义ascx控件导航控件中调用NavigationTab_Click,当我单击该按钮时,在每个控件上动态加载的ctrl控件对象都有按钮和标签,它只是将一些文本重新分配给标签.

I am calling NavigationTab_Click from my navigation control that is another custom ascx control, my ctrl Control object that is loading dynamically on each has button and label when I click the button it simply reassigns some text to the label.

我在主页上有以下代码,只是为了获取ascx控件路径:

and I have this following code on my masterpage just to get the ascx control path:

protected override void OnInit(EventArgs e)
        {           
               //raising an event to set ascx path
                mainmenu.NavigatePath += new usercontrols.mainmenu.NavigationHandler(NavigationTab_Click);

                base.OnInit(e);          
        } 

到目前为止,一切正常,通过调用NavigationTab_Click函数加载ctrl对象后,我在占位符中看到了ctrl并具有按钮和标签,但是问题是如果我单击此按钮,它应该将标签重新分配给某些文本但是相反,整个ctrl控件的内容都消失了,请帮忙.

so far everything works good, after loading my ctrl object by calling NavigationTab_Click function I see my ctrl in the placeholder and has the button and the label but the issue is this if I click this button it should reassign the label to some text but instead the whole ctrl control content disappears, please help.

推荐答案

动态添加控件时,必须确保在每次回发时都重新创建它.您还必须确保分配的ID与以前相同,否则将无法正确触发事件,并且无法从ViewState重新加载值.必须最迟在Page_Load(最好在Page_Init中)完成此操作.

When you're adding controls dynamically you must ensure that it gets recreated on every postback. You also have to ensure that you assign the same ID as before, otherwise events will not be triggered correctly and values cannot be reloaded from ViewState. This must be done it Page_Load at the latest(better in Page_Init).

这就是为什么应该尽可能避免动态控制的原因.

That's the reason why you should avoid dynamical controls whenever possible.

因此,您可以像完成操作一样在事件处理程序中添加控件.但是必须在下一次回发中重新创建它们.因此,您需要将某处(例如ID)或已创建的控件存储在某处.例如,可以在ViewStateSession中完成.然后,您可以为控件分配适当的ID(例如,带有索引或ID的后缀).

So you can add controls in event-handlers like you've done. But they must be recreated on the next Postback. So you need to store somewhere what(f.e. IDs) or how many controls are already created. That can be done for example in ViewState or Session. Then you can assign appropriate IDs to the controls(for example with the index or ID suffixed).

以下是有关此主题的一些其他信息:

Here are some additional informations on this subject:

  • View State and Dynamically Added Controls *
  • ASP.NET Page Life Cycle Overview

这篇关于单击按钮回发后,UpdatePanel中的自定义usercontrol内容消失的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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