Saturday, October 8, 2011

Access web control on Nested master page from content page

For example I have a DropDownList control in the inner master page. This control can be accessed from the content page in 2 ways.

  1. Using the findcontrol method to access the DropDownList, you need to use the DropDownList's ClientID.

    For example :

    DropDownList list = (DropDownList)this.Master.FindControl("ctl00$ctl00$ContentPlaceHolder1$DropDownList1");

  2. Expose the DropDownList control in the inner master page's code bhind, then the control can accessed directly.
    • Expose the DropDownList control:

        public DropDownList list
                    return this.DropDownList1;


  • Add the masterType for the content page to strong type the master page:

    <%@ MasterType VirtualPath="~/Masterpage/NestMasterPage/MasterPage2.master" %>


  • Access the DropDownList in the content page's codebehind:

            DropDownList list = this.Master.list;

Happy Coding…

No comments:

Post a Comment