Master Pages in ASP.Net 2.0 - Content Pages
(Page 3 of 4 )
To define and populate a content page, the process is equally as painless. In the @Page directive, you simply need to indicate which Master Page the content page uses. Then with the page, you implement any controls (content) by placing them inside a "content" control, and indicate which content placeholder you’re filling. So the content page could be as simple as the following:
<%@ Page Language=”C#” masterpagefile=”site.master” %>
<asp:content runat=”server” contentplaceholderID=”bodyContents”>
<p>this is some content to replace the default content</p>
</asp:content>
So you could save this as default.aspx, run the application, and the result would be a merge of the master and content pages, with the logo and the new paragraph attached.
Something you may have noticed is that on default.aspx, I didn’t include the necessary <form runat=”server”> control. This may seem strange to you, but I assure you it’s not an error. The reason for this is that the form control is already implemented in the Master Page, and the result of the merging is that the content page inherits the form from its parent Master Page. It makes sense when you think about it!
But the whole point of ASP.Net is not to put up static content on the web, but to create dynamic pages. So how would you do that in a content page? Well, the programmatic code still exists outside of the layout, and your code will take the following form:
<%@ Page Language=”C#” masterpagefile=”site.master” %>
<script runat=”server”>
void calcClicks(object sender, EventArgs e)
{
int count = int.Parse(clickedLbl.Text)
clickedLbl.Text = count++;
}
</script>
<asp:content runat=”server” contentplaceholderID=”bodyContents”>
<p>this is some content to replace the default content</p>
<asp:button runat=”server” id=”clickBtn” text=”click”
Onclick=”calcClicks” /><br />
This button has been clicked
<asp:label id=”clickedLbl” runat=”server”>0</asp:label>
times.
</asp:content>
So we’ve thrown in some fairly simplistic code to count how many times a button has been clicked. Not brain surgery, but at least you can see how in-page coding will work. And there is no change to the code-behind page, so if you’re accustomed to working with that, you have nothing to learn anew!
Next: Going Beyond the Basics >>
More ASP.NET Articles
More By Justin Cook