Article

Constructing ASP.NET Web Pages

Page: 1 2 3 4 5 6 7

After we register the control, we create instances of it using the <TagPrefix:TagName> format. Let’s try an example that uses the SmartBox control. Create a Web Form named ControlTest.aspx in your project folder, and give it this content:

Visual Basic LearningASP\VB\ControlTest.aspx
<%@ Page Language="VB" %>        
<%@Register TagPrefix="sp" TagName="SmartBox"      
   Src="SmartBox.ascx" %>      
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"      
   "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">      
     
<script runat="server">      
     
</script>      
     
<html xmlns="http://www.w3.org/1999/xhtml">      
 <head runat="server">      
   <title>Creating ASP.NET Web Server Controls</title>      
 </head>      
 <body>      
   <form id="form1" runat="server">      
   <div>      
     <sp:SmartBox ID="nameSb" runat="server"      
         LabelText="Name:" />      
     <sp:SmartBox ID="addressSb" runat="server"      
         LabelText="Address:" />      
     <sp:SmartBox ID="countrySb" runat="server"      
         LabelText="Country:" />      
     <sp:SmartBox ID="phoneSb" runat="server"      
         LabelText="Phone:" />
     
   </div>      
   </form>      
 </body>      
</html>

Creating this page by hand will really help you to understand the process of building a web form. In time, you’ll learn how to use Visual Web Developer to do part of the work for you. For example, you can drag a user control from Solution Explorer and drop it onto a web form; Visual Web Developer will register the control and add an instance of the control for you. Loading this page will produce the output we saw in Figure 10.

Now, this is a very simple example indeed, but we can easily extend it for other purposes. You can see in the above code snippet that we set the LabelText property directly using the control’s attributes; however, we could have accessed the properties from our code instead. Here’s an example of in which we set the LabelText properties of each of the controls using VB and C#:

Visual Basic
<script runat="server">      
 Protected Sub Page_Load()      
   nameSb.LabelText = "Name:"      
   addressSb.LabelText = "Address:"      
   countrySb.LabelText = "Country:"      
   phoneSb.LabelText = "Phone:"      
 End Sub      
</script>

C#
script runat="server">      
 protected void Page_Load()      
 {      
   nameSb.LabelText = "Name:";      
   addressSb.LabelText = "Address:";      
   countrySb.LabelText = "Country:";      
   phoneSb.LabelText = "Phone:";      
 }      
</script>

Master Pages

Master pages are an important feature that was introduced in ASP.NET 2.0. Master pages are similar to web user controls in that they, too, are composed of HTML and other controls; they can be extended with the addition of events, methods, or properties; and they can’t be loaded directly by users—instead, they’re used as building blocks to design the structure of your web forms.
A master page is a page template that can be applied to give many web forms a consistent appearance. For example, a master page can set out a standard structure containing the header, footer, and other elements that you expect to display in multiple web forms within a web application.

Master page files have the .master extension, and, just like web forms and web user controls, they support code-behind files. All master pages inherit from the class System.Web.UI.MasterPage.

Designing a site structure using master pages and web user controls gives you the power to modify and extend the site easily. If your site uses these features in a well-planned way, it can be very easy to modify certain details of the layout or functionality of your site, because updating a master page or a web user control takes immediate effect on all the web forms that use the file.

As we’ve already mentioned, a master page is built using HTML and controls, including the special ContentPlaceHolder control. As its name suggests, the ContentPlaceHolder is a placeholder that can be filled with content that’s relevant to the needs of each web form that uses the master page. In creating a master page, we include all of the basic structure of future pages in the master page itself, including the <html>, <head>, and <body> tags, and let the web forms specify the content that appears in the placeholders.

Let’s see how this works with a simple example. Suppose we have a site with many pages, all of which contain a standard header, footer, and navigation menu, laid out as per the wireframe shown in Figure 12.

A simple web site layout

If all the pages in the site have the same header, footer, and navigation menu, it makes sense to include these components in a master page, and to build several web forms that customize only the content areas on each page.

To keep this example simple, we won’t include a menu here: we’ll include just the header, the footer, and the content placeholder. Add a new file to your project using the Master Page template in Visual Web Developer. Name the file FrontPages.master, as shown in Figure 13, and select the appropriate language. You’ll notice that some <asp:ContentPlaceHolder> tags have been created for you already, one in the <head>, and one in the page body. You can remove them and modify the page like this:

Visual Basic LearningASP\VB\FrontPages.master
<%@ Master Language="VB" %>      
     
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"      
   "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">      
     
<script runat="server">      
     
</script>      
     
<html xmlns="http://www.w3.org/1999/xhtml">      
 <head runat="server">      
   <title>Untitled Page</title>      
 </head>      
 <body>      
   <form id="form1" runat="server">      
   <div>      
     <h1>Welcome to SuperSite Inc!</h1>      
     <asp:ContentPlaceHolder ID="FrontPageContent"      
         runat="server" />      
     <p>Copyright 2006</p>        
   </div>      
   </form>      
 </body>      
</html>

Again, the C# version is identical except for the Master declaration at the top of the page:

C# LearningASP\CS\FrontPages.master (excerpt)
<%@ Master Language="C#" %>

Creating a Master Page

The master page looks almost like a web form, except for one important detail: it has an empty ContentPlaceHolder control. If you want to build a web form based on this master page, you just need to reference the master page using the Page directive in the web form, and add a Content control that includes the content you want to insert.

Let’s try it. Create a web form in Visual Web Developer called FrontPage.aspx, and check the Select master page option. This option will allow you to choose a master page for the form; Visual Web Developer will then use that master page to generate the code for you. Edit it to match this code snippet:

Visual Basic LearningASP\VB\FrontPage.aspx (excerpt)
<%@ Page Language="VB" MasterPageFile="~/FrontPages.master"      
   Title="Front Page" %>      
     
<script runat="server">      
     
</script>      
     
<asp:Content ID="Content1" ContentPlaceHolderID="FrontPageContent"      
   Runat="Server">      
 <p>      
   Welcome to our web site! We hope you'll enjoy your visit.      
 </p>      
</asp:Content>

The VB and C# versions of this code are the same except for the Language attribute on the Page declaration, but because Visual Web Developer generates all the code for you automatically, you don’t need to worry about it—instead, you can focus on the content.

You’re all set now! Executing FrontPage.aspx will generate the output shown in Figure 14.

Using a master page to set the header and footer

Although the example is simplistic, it’s easy to see the possibilities: you can create many web forms based on this template very easily. In our case, the master page contains a single ContentPlaceHolder, but it could have more. Also, we can define within the master page default content for display inside the ContentPlaceHolder on pages whose web forms don’t provide a Content element for that placeholder.

We’ll explore Visual Web Developer’s capabilities in more depth in the following chapters, but for now you can play around with it yourself, using Design mode to visually edit web forms that are based on master pages. Looking at Figure 15, you can see that the content of the master page is read-only, and that you can edit only the content areas of the page.

Design mode shows the editable areas of a form that uses a master page

Summary

In this chapter, we discussed web forms, HTML server controls, web server controls, web user controls, master pages, and CSS. All these elements can be combined to create powerful structures for your web sites.

In the next chapter, we start building “real” web applications, putting into practice most of the theory you’ve learned so far, and using a professional development environment that will do part of the work for you—and all this can be found in Build Your Own ASP.NET 3.5 Web Site Using C# & VB, in stock now!

If you liked this article, share the love:
Print-Friendly Version Suggest an Article

Sponsored Links

Rate This Article

  • 1
    Poor
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
    Great

Comment on This Article

Have something to say?

Post A Comment

You need to be a member of the SitePoint Forums to comment on this post. Sign Up

Already a member? Post using your SitePoint Forums account: