How to use Masterpages and Contentpages in ASP.NET 2.0? What is a Masterpage? What is a ContentPage?
How to setup master-content pages in ASP.NET 2.0?
A Master Page is the mother of the page and the content page is the baby! There may be many babies of the mother.
A master page may have plenty of content pages within it. There also may be a master page within a
master page, which is called a nested master page. Nested Master Pages
So whats a master page in ASP.NET 2.0? A Master Page is a page that contains markup and controls that are shared
across multiple pages in your site. For example, if required that all pages should have the same header and footer banners
or the common navigation menu, the Master page can be written once, and all the content pages can access the common master page.
The content pages inherit the tags inside the master page.
Next question is how to define a master page. The answer is...
To define a Master Page, it may be written like a normal page. Master Pages can contain markup, controls, or code, or
any combination of these tags. The Content pages are rendered in a master page control called as
ContentPlaceHolder control. A ContentPlaceHolder defines a region of the master page rendering that can be
exchanged with content from a page associated to the master. A ContentPlaceHolder can also contain default content,
when in case the derive page does not need to override this content. Below is the markup of how to use a
Contentplaceholder.
<asp:contentplaceholder id="ContentPlaceHolderInterviewQuestions" runat="server"/>
<asp:contentplaceholder id="ContentPlaceHolderInterviewQuestions" runat="server">
<h3>Welcome to the .NET Interview Questions website!</h3>
</asp:contentplaceholder>
A master page has the extension .master, unlike a normal web form that has the extenstion .aspx.
A page may derive from a Master Page by defining a MasterPageFile attribute on its Page directive.
Below is the markup of a content page.
<%@ Page MasterPageFile="Masterpage.master" %>
<asp:content id="Content1" contentplaceholderid="ContentPlaceHolderInterviewQuestions" runat="server">
Here goes contents of ContentPlaceHolderInterviewQuestions </asp:content>
</asp:content>
So how does Content page access a master page?
It is also possible for a Content Page to programmatically access a Master Page.
A ContentPage may create a strongly-typed reference to the Master Page using the <%@ MasterType %>
directive, which specifies the virtual path to the masterpage as below...
<%@ MasterType VirtualPath="Masterpage.master" %>
The Content Page may then reference the Master Page using the Master property of the Page class:
Master.FooterText = "Some text on the footer of .NET Interview Questions page";
Label lb1 = Master.FindControl("label2");
As with the example above, FooterText is a public property exposed on the Master Page, whereas label2
is a server side control on the Master Page.
Note that the FindControl method is a very useful and a very important method, that may be used for
finding a control in a content page from a master page and vice versa.
Bind data to a server control without writing code
Masterpage ContentPage
Nested Masterpage?
SiteMapPath control
Sort GridView
Hotspot class, ImageMap
Update Delete Gridview, Datakeynames