Archive for November 5th, 2007

How to properly access controls in the Master Page

One of the great new features in ASP.NET 2.0 has been the ability to use master pages to layout the common elements of the web site.  This certainly works better than top and bottom include files or top and bottom Web User Controls.  But at least with the user controls, if we wanted to access a control within it, the method to do so was pretty straight forward.  All you had to do was expose the control as a public property.  Or, better, create a pass through property on the control that sets or retrieves the inner control’s property.  By doing this, you could turn a side bar element on or off (as an example.)

While you can expose elements of a master page in a similar manner, anyone who’s done this knows there are a few gotchas along the way.  I’m going to tell you how to avoid the gotchas and you’ll have the added benefit of being able to access the controls on the master page even if you decide to use a different master page without having to change any code other than the master page directive at the top of the aspx page.

Read the rest of this entry »