简体   繁体   English

如果在Page_Load中添加了UpdatePanel,则无法在ascx控件中使用

[英]UpdatePanel not working in ascx control if is added on Page_Load

I have an ascx control that I am loading it on masterpage's Page_Load(), in my ascx control I have my asp UpdatePanel. 我有一个ascx控件,正在将它加载到母版页的Page_Load()上,在我的ascx控件中,我有我的asp UpdatePanel。

Loading ascx in master page: 在母版页中加载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);             
     }

the issue is this: if I load the usercontrol this way my UpdatePanel that is inside the masterpage is not working, but if I add register tag in my masterpage as bellow code and import the ascx that way UpdatePanel works normal. 问题是这样的:如果我以这种方式加载用户控件,则主页中的UpdatePanel无法正常工作,但是如果我在主页中添加注册标签作为下面的代码,并以这种方式导入ascx,则UpdatePanel可以正常工作。

<%@ Register Src="~/admin/usercontrols/contentexplorer.ascx" TagName="Tree" TagPrefix="NAV" %>

<NAV:Tree ID="treenav" runat="server" />

I assume I might need to load the control in different page life cycle event, I did try Page_Init but did not work, please help. 我假设我可能需要在不同的页面生命周期事件中加载控件,我确实尝试过Page_Init,但没有用,请帮忙。

As pointed before, PreInit does not exist in a MasterPage. 如前所述,PreInit在母版页中不存在。 However, it is not necessary. 但是,这不是必需的。 Just make sure that you are adding your UserControl as a child control of the UpdatePanel's ContentTemplateContainer: 只需确保将UserControl添加为UpdatePanel的ContentTemplateContainer的子控件即可:

protected void Page_Load(object sender, EventArgs e)
{
    WebUserControl1 ctrl = (WebUserControl1)LoadControl("~/WebUserControl1.ascx");
    UpdatePanel1.ContentTemplateContainer.Controls.Add(ctrl);
}

Hope it helps! 希望能帮助到你!

I'm not sure what you mean when you say "not working" (what's not working?), but remember to set the ID of your control before adding it to the Control list or else events might not be executed correctly: 我不确定当您说“不起作用”(什么不起作用?)时是什么意思,但是请记住在将控件添加到“控件”列表之前设置控件的ID,否则事件可能无法正确执行:

protected void Page_Load(object sender, EventArgs e)
{
    WebUserControl1 ctrl = (WebUserControl1)LoadControl("~/WebUserControl1.ascx");
    ctrl.ID = "controlId";
    UpdatePanel1.ContentTemplateContainer.Controls.Add(ctrl);
}

You might want to add it on the PreInit event. 您可能要在PreInit事件上添加它。 Read this blog post as it discusses what you need. 阅读博客文章,讨论您的需求。

EDIT: 编辑:

As @Tim pointed out since you are trying to do this in a masterpage you don't have a PreInit event. 正如@Tim指出的那样,由于您试图在母版页中执行此操作,因此没有PreInit事件。 You could use a trick (or this )as a workaround, but generally you don't have much of a choice. 您可以使用技巧 (或 技巧 )作为解决方法,但是通常您没有太多选择。

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

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