Accessing User Controls in Master Pages – ASP.NET 2.0
I love the idea of master pages in ASP.NET 2.0. They provide a standard look and feel for web applications that work much better than the old “include” model.
Sometimes it still makes sense to have a user control embedded in the master page. In particular, toolbars and navigation items may be reused across master and other non-master pages. Here’s how to access the user control on the master page:
On the child .aspx page, add the following:
<%@Reference Control="~/UserControls_Toolbar.ascx" %>
This allows the control to be referenced by the child.
In the code behind for the child you can now retrieve the object using:
UserControls_ToolBar Toolbar = (UserControls_ToolBar)Master.FindControl("Toolbar");
From there you can reference any properties on Toolbar as normal.
Its also recommended to create public property references on the master page for easier access to the user control (via Kiran Chand).
One Comment
- Aaron Edwards said:
