UpdatePanel no funciona en el control ascx si se agrega en 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.

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.

preguntado el 09 de marzo de 12 a las 23:03

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

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

3 Respuestas

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);
}

¡Espero eso ayude!

respondido 10 mar '12, 02:03

Hi and thank, my controls are not in UpdatePanel not sure why you said that? UpdatePanel has a label and button and all of my ascx controls are loading in placeholders that are in masterpage, just to be clear I am not trying to load ascx in UpdatePanel - Nazo Tajrián

So, is your problem not getting the event handler for the button inside the UserControl raised? - Reinaldo

yes that is the problem, if I load the control in placeholder.Controls.Add() the button in updatePanel not raise ajax call, but if I register it in the masterpage with "<@ Register" tag button in updatePanel works good. - Nazo Tajrián

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);
}

respondido 10 mar '12, 03:03

You might want to add it on the PreInit evento. Leer este 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 truco (o este)as a workaround, but generally you don't have much of a choice.

respondido 09 mar '12, 23:03

No hay PreInit in MasterPage. - Tim Schmelter

Generally he tiene the choice to add it declaratively instead of dynamically what is always the better choice ;) - Tim Schmelter

@TimSchmelter - absolutely true, I was just trying to help in the context he provided. - elboyan

-1 is from my side. Preinit is not the part of master page. @Tim- OP might have to take some on the fly decisions... - Pankaj

@PankajGarg - read the links in the edit, he could use some trickery to get to a PreInit event in a page. Generally there is no easy way to do what he is trying to do in the masterpage itself. - elboyan

No es la respuesta que estás buscando? Examinar otras preguntas etiquetadas or haz tu propia pregunta.