locked
Load a usercontrol into masterpage RRS feed

  • Question

  • User1572048940 posted
    hi, I am having a master page and set of usercontrols. Initially i load a usercontrol into contentplaceholder of master page. I need to load another usercontrol on click of a button in that user control. ie adding usercontrol from usercontrol on master page
    Sunday, January 25, 2009 8:56 AM

Answers

  • User-240597641 posted

    You can dynamically add a user control by using loadcontrol method.

    add a PlaceHolder into your master page and on the click event of your parent user control add this code

            Control uc = LoadControl("ChildUserControl.ascx");
            MasterPage mainPage = this.Parent.Page.Master;
            PlaceHolder ph = mainPage.FindControl("placeHolder") as PlaceHolder;
            ph.Controls.Add(uc);
     May this help you...
     
      
    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Sunday, January 25, 2009 3:36 PM
  • User-2115483147 posted

    You can dynamically add a user control by using loadcontrol method.

    add a PlaceHolder into your master page and on the click event of your parent user control add this code

            Control uc = LoadControl("ChildUserControl.ascx");
            MasterPage mainPage = this.Parent.Page.Master;
            PlaceHolder ph = mainPage.FindControl("placeHolder") as PlaceHolder;
            ph.Controls.Add(uc); 

      

    Additionally,

    This thread will show you how to definde an evevnt inside of a user control and implement the event on page(MasterPage),

    http://forums.asp.net/t/1218643.aspx

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Thursday, January 29, 2009 1:15 AM

All replies

  • User-240597641 posted

    You can dynamically add a user control by using loadcontrol method.

    add a PlaceHolder into your master page and on the click event of your parent user control add this code

            Control uc = LoadControl("ChildUserControl.ascx");
            MasterPage mainPage = this.Parent.Page.Master;
            PlaceHolder ph = mainPage.FindControl("placeHolder") as PlaceHolder;
            ph.Controls.Add(uc);
     May this help you...
     
      
    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Sunday, January 25, 2009 3:36 PM
  • User-2115483147 posted

    You can dynamically add a user control by using loadcontrol method.

    add a PlaceHolder into your master page and on the click event of your parent user control add this code

            Control uc = LoadControl("ChildUserControl.ascx");
            MasterPage mainPage = this.Parent.Page.Master;
            PlaceHolder ph = mainPage.FindControl("placeHolder") as PlaceHolder;
            ph.Controls.Add(uc); 

      

    Additionally,

    This thread will show you how to definde an evevnt inside of a user control and implement the event on page(MasterPage),

    http://forums.asp.net/t/1218643.aspx

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Thursday, January 29, 2009 1:15 AM
  • User-801184863 posted
    hi, I am having a master page and set of usercontrols. Initially i load a usercontrol into contentplaceholder of master page. I need to load another usercontrol on click of a button in that user control. ie adding usercontrol from usercontrol on master page

     

    Tuesday, March 23, 2010 7:53 AM
  • User-1272609167 posted

    You can try the below code.

      if (this.Master.FindControl("ContentPlaceHolder1")!=null)
        {
                    Control uc = LoadControl("mycontrol.ascx");
                    this.Master.FindControl("ContentPlaceHolder1").Controls.Add(uc); 
        }

    Wednesday, July 7, 2010 5:21 AM