0
votes

I have an ascx control that I am loading it on masterpage's Page_Load(), in my ascx control I have my asp UpdatePanel.

Loading ascx in master page:

     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.

<%@ 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.

3
What means "it did not work" when loading it in Page_Init? Why do you add it dynamically if it's not required at all? You're making your life more difficult than necessary.Tim Schmelter
Means UpdatePanel not working as expected! I have a button and label in the UpdatePanel content, the button is the trigger that has a function if you click it should change the text of the label with out reloading the whole page which is the way that UpdatePanel works, so not working means not changing the text as it shouldNazo Tajrian

3 Answers

0
votes

As pointed before, PreInit does not exist in a MasterPage. However, it is not necessary. Just make sure that you are adding your UserControl as a child control of the UpdatePanel's ContentTemplateContainer:

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

Hope it helps!

0
votes

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:

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

You might want to add it on the PreInit event. 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. You could use a trick (or this)as a workaround, but generally you don't have much of a choice.