How do I use the user control dynamically in master page? RRS feed

  • Question

  • ı have a master page, index.aspx and makale.ascx

    click on the buttons in masterpage I want to add a new user control in index.aspx page


    Tuesday, May 28, 2013 8:52 AM


  • 1. Add a PlaceHolder on the aspx page:

    <asp:PlaceHolder ID="container" runat="server" />

    2. Use the FindControl method on the ContentPlaceHolder in the MasterPage to find the PlaceHolder and add an instance of your UserControl it it:

    protected void MasterPageButton_Click(object sender, EventArgs e)
        PlaceHolder container = (PlaceHolder)ContentPlaceHolder1.FindControl("container");
        if (container != null)
            YourUserControl control = (YourUserControl)LoadControl("~/Controls/control.ascx");

    • Proposed as answer by Mike Feng Wednesday, May 29, 2013 2:37 AM
    • Marked as answer by Mike Feng Friday, May 31, 2013 10:37 AM
    Tuesday, May 28, 2013 10:47 AM