Sunday 11 April 2010

ASP.Net Master Page Control Tip

If you are using Master Pages for your website and wish to control the content of them from your content page then you need to know about the MasterType directive.

Simply place the following piece of code at the top of any content page.

<%@ MasterType virtualpath="~/Default.master" %>

just replace the virtualpath with the actual path to your master page, and there you have it.

You can now control server controls and elements on your Master page from your content pages by simply calling the Master in your source code.

For Example

Lets say you want to find a panel from a content page that actually lives on your master page, then you would just do the following.

Dim SomePanel As Panel = Master.FindControl("SomePanel")

This simply uses FindControl to locate the Panel with an ID of SomePanel, although the principle is the same for any other control on your Master page.

So play around and see how easy it is to control all of your website.
Online Cashback