![](/img/trans.png)
[英]Problems maintaining a custom control's (ascx) states between postbacks when loading said ascx control in page_load
[英]UpdatePanel not working in ascx control if is added on Page_Load
我有一个ascx控件,正在将它加载到母版页的Page_Load()上,在我的ascx控件中,我有我的asp UpdatePanel。
在母版页中加载ascx:
protected void Page_Load(object sender, EventArgs e)
{
usercontrols.mainmenu adminmenu = (usercontrols.mainmenu)LoadControl("~/mymenupath.ascx");
//phmainmanu is a placeholder in masterpage
phmainmanu.Controls.Add(adminmenu);
}
问题是这样的:如果我以这种方式加载用户控件,则主页中的UpdatePanel无法正常工作,但是如果我在主页中添加注册标签作为下面的代码,并以这种方式导入ascx,则UpdatePanel可以正常工作。
<%@ Register Src="~/admin/usercontrols/contentexplorer.ascx" TagName="Tree" TagPrefix="NAV" %>
<NAV:Tree ID="treenav" runat="server" />
我假设我可能需要在不同的页面生命周期事件中加载控件,我确实尝试过Page_Init,但没有用,请帮忙。
如前所述,PreInit在母版页中不存在。 但是,这不是必需的。 只需确保将UserControl添加为UpdatePanel的ContentTemplateContainer的子控件即可:
protected void Page_Load(object sender, EventArgs e)
{
WebUserControl1 ctrl = (WebUserControl1)LoadControl("~/WebUserControl1.ascx");
UpdatePanel1.ContentTemplateContainer.Controls.Add(ctrl);
}
希望能帮助到你!
我不确定当您说“不起作用”(什么不起作用?)时是什么意思,但是请记住在将控件添加到“控件”列表之前设置控件的ID,否则事件可能无法正确执行:
protected void Page_Load(object sender, EventArgs e)
{
WebUserControl1 ctrl = (WebUserControl1)LoadControl("~/WebUserControl1.ascx");
ctrl.ID = "controlId";
UpdatePanel1.ContentTemplateContainer.Controls.Add(ctrl);
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.