views:

1014

answers:

2

I have a masterpage with an update panel:

<asp:UpdatePanel ID="UpdatePanel" runat="server" ChildrenAsTriggers="true" EnableViewState="False"
                UpdateMode="Conditional">
                <ContentTemplate>
                    <div id="mainContent">
                        <asp:ContentPlaceHolder ID="ContentPlaceHolder1" runat="server">
                        </asp:ContentPlaceHolder>
                    </div>
                </ContentTemplate>
                <Triggers>
              </Triggers>
            </asp:UpdatePanel>

Then I have Default.aspx page which uses the masterpage file:

<asp:Content ID="Content1" runat="server" ContentPlaceHolderID="ContentPlaceHolder1">
<asp:PlaceHolder ID="plhCurrentItem" runat="server"></asp:PlaceHolder>
</asp:Content>

I programmatically load usercontrols into the placeholder with id plhCurrentItem.

The problem is when I click a button in the usercontrol, no event fires. The usercontrol just disappears and the updatepanel is left blank.

What am I doing wrong?


Update

Code used to add usercontrols. The LoadControls method is called from the Page_load event.

 Control ctlCurrentItem = null;

public string currentControl
{
    get { return ((string)Session["currentControl"]); }
    set { Session["currentControl"] = value; }
}



public void LoadControls()
{
    switch (currentControl)
    {
        case "Home":
            ctlCurrentItem = Page.LoadControl("~/pages/Home.ascx");
            ctlCurrentItem.ID = "Home";
            break;
        case "Resume":
            ctlCurrentItem = Page.LoadControl("~/pages/Resume.ascx");
            ctlCurrentItem.ID = "Resume";
            break;
        case "Projects":
            ctlCurrentItem = Page.LoadControl("~/pages/Projects.ascx");
            ctlCurrentItem.ID = "Projects";
            break;
        case "Contact":
            ctlCurrentItem = Page.LoadControl("~/pages/Contact.ascx");
            ctlCurrentItem.ID = "Contact";
            break;
        default:
            return;

    }
    plhCurrentItem.Controls.Clear();
    plhCurrentItem.Controls.Add(ctlCurrentItem);


}
+1  A: 

Put LoadControls call in the OnPreInt event from the page life-cycle:

Use this event for the following:

  • Check the IsPostBack property to determine whether this is the first time the page is being processed.
  • Create or re-create dynamic controls.
  • Set a master page dynamically.
  • Set the Theme property dynamically.
  • Read or set profile property values.
rick schott
A: 

Put usercontrol in the panel this will solve your problem

a23