Chapter 1. Overview of the ASP.NET Framework


In this Chapter

  • ASP.NET and the .NET Framework

  • Understanding ASP.NET Controls

  • Understanding ASP.NET Pages

  • Installing the ASP.NET Framework

  • Summary

Let's start by building a simple ASP.NET page.

Note

For information on installing ASP.NET, see the last section of this chapter.


If you are using Visual Web Developer or Visual Studio .NET, you first need to create a new website. Start Visual Web Developer and select the menu option File, New Web Site. The New Web Site dialog box appears (see Figure 1.1). Enter the folder where you want your new website to be created in the Location field and click the OK button.

Figure 1.1. Creating a new website.


After you create a new website, you can add an ASP.NET page to it. Select the menu option Website, Add New Item. Select Web Form and enter the value FirstPage.aspx in the Name field. Make sure that both the Place Code in Separate File and Select Master Page check boxes are unchecked, and click the Add button to create the new ASP.NET page (see Figure 1.2).

Figure 1.2. Adding a new ASP.NET page.


The code for the first ASP.NET page is contained in Listing 1.1.

Listing 1.1. FirstPage.aspx

<%@ Page 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">     Sub Page_Load()         lblServerTime.Text = DateTime.Now.ToString()     End Sub </script> <html xmlns="http://www.w3.org/1999/xhtml" > <head>     <title>First Page</title> </head> <body>     <form  runat="server">     <div>     Welcome to ASP.NET 2.0! The current date and time is:     <asp:Label                  Runat="server" />     </div>     </form> </body> </html> 

Note

The CD that accompanies this book contains C# versions of all the Visual Basic .NET code samples.


The ASP.NET page in Listing 1.1 displays a brief message and the server's current date and time. You can view the page in Listing 1.1 in a browser by right-clicking the page and selecting View in Browser (see Figure 1.3).

Figure 1.3. Viewing FirstPage.aspx in a browser.


The page in Listing 1.1 is an extremely simple page. However, it does illustrate the most common elements of an ASP.NET page. The page contains a directive, a code declaration block, and a page render block.

The first line, in Listing 1.1, contains a directive. It looks like this:

<%@ Page Language="VB" %> 


A directive always begins with the special characters <%@ and ends with the characters %>. Directives are used primarily to provide the compiler with the information it needs to compile the page.

For example, the directive in Listing 1.1 indicates that the code contained in the page is Visual Basic .NET (VB .NET) code. The page is compiled by the Visual Basic .NET compiler and not another compiler such as the C# compiler.

The next part of the page begins with the opening <script runat="server"> tag and ends with the closing </script> tag. The <script> tag contains something called the code declaration block.

The code declaration block contains all the methods used in the page. It contains all the page's functions and subroutines. The code declaration block in Listing 1.1 includes a single subroutine named Page_Load(), which looks like this:

Sub Page_Load()   lblServerTime.Text = DateTime.Now.ToString() End Sub 


This subroutine assigns the current date and time to the Text property of a Label control contained in the body of the page named lblServerTime.

The Page_Load() subroutine is an example of an event handler. This subroutine handles the Page Load event. Each and every time the page loads, the subroutine automatically executes and assigns the current date and time to the Label control.

The final part of the page is called the page render block. The page render block contains everything that is rendered to the browser. In Listing 1.1, the render block includes everything between the opening and closing <html> tags.

The majority of the page render block consists of everyday HTML. For example, the page contains the standard HTML <head> and <body> tags. In Listing 1.1, there are two special things contained in the page render block.

First, notice that the page contains a <form> tag that looks like this:

<form  runat="server"> 


This is an example of an ASP.NET control. Because the tag includes a runat="server" attribute, the tag represents an ASP.NET control that executes on the server.

ASP.NET pages are often called web form pages because they almost always contain a server-side form element.

The page render block also contains a Label control. The Label control is declared with the <asp:Label> tag. In Listing 1.1, the Label control is used to display the current date and time.

Controls are the heart of the ASP.NET framework. Most of the ink contained in this book is devoted to describing the properties and features of the ASP.NET controls.

Controls are discussed in more detail shortly. However, first you need to understand the .NET Framework.

Note

By default, ASP.NET pages are compatible with the XHTML 1.0 Transitional standard. You'll notice that the page in Listing 1.1 includes an XHTML 1.0 Transitional DOCTYPE. For details on how the ASP.NET framework complies with both XHTML and accessibility standards, see my article at the Microsoft MSDN website (msdn.Microsoft.com), entitled "Building ASP.NET 2.0 Web Sites Using Web Standards."





ASP. NET 2.0 Unleashed
ASP.NET 2.0 Unleashed
ISBN: 0672328232
EAN: 2147483647
Year: 2006
Pages: 276

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net