Friday, April 10, 2009

Working with ASP.NET Master pages dynamically

There are two primary things we can do using ASP.NET Master pages other than using them for consistent look and feel across the web application.
 
  • Accessing the member of the master page from within the content page
  • Changing the master page of the content page dynamically.
For a given content page, we usually use the following Page directive to specify the master file.
<%@ Page MasterPageFile="~/MyTextMaster.master" %>
 
Now, to access any of the available members of the master page, we need to use FindControl method, and locate the required control and access. So, that means there is no strong typed way of accessing any members of a master page within the content page.
 
@MasterType directive does exactly that. If a page is setup with a mastertype directive, when the page creates its Master property, the property is typed to the referenced master type. This is how it can be used.
 
<%@ MasterType VirtualPath="~/MyTextMaster.master" %>

No comments:

Post a Comment