- ASP.NET and the .NET Framework (inactive)
- Introducing ASP.NET Controls
- Adding Application Logic to an ASP.NET Page
- The Structure of an ASP.NET Page
- Summary
The Structure of an ASP.NET Page
You are now in a position to examine the formal structure of an ASP.NET page. The following is a list of the important elements of an ASP.NET page:
- Directives
- Code declaration blocks
- ASP.NET controls
- Code render blocks
- Server-side comments
- Server-side include directives
- Literal text and HTML tags
Each element is discussed in the following sections.
Directives
A directive controls how an ASP.NET page is compiled. The beginning of a directive is marked with the characters <%@ and the end of a directive is marked with the characters %>. A directive can appear anywhere within a page. By convention, however, a directive typically appears at the top of an ASP.NET page.
There are several types of directives that you can add to an ASP.NET page. Two of the most useful types are page and import directives.
Page Directives
You can use a page directive to specify the default programming language for a page. Page directives can also be used to enable tracing and debugging for a page.
To change the default programming language of an ASP.NET page from Visual Basic to C#, for example, you would use the following page directive:
<%@ Page Language="C#" %>
NOTE
The keyword Page in a page directive is optional. The following two directives are equivalent:
<%@ Page Language="C#" %> <%@ Language="C#" %>
Another extremely useful page directive is the Trace directive. If you enable tracing for a page, additional information about the execution of the page is displayed along with the content of the page (see Figure 5). You can enable tracing for a page by including the following directive:
<%@ Page Trace="True" %>
After you enable tracing for a page, you can display trace messages by using two methods of the Trace class: Write() and Warn(). You can use either method to display a custom message in the trace information displayed at the bottom of the page. The only difference between the two methods is that the former method displays messages in black text, and the latter method displays messages in red text, which is easier to see.
Figure 5 Enabling page tracing.
The ASP.NET page in Listing 11 uses these methods to display various trace messages.
Listing 11Trace.aspx
<%@ Page Trace="True" %> <Script Runat="Server"> Sub Page_Load Dim strTraceMessage As String Trace.Warn( "Page_Load event executing!" ) strTraceMessage = "Hello World!" Trace.Write( "The value of strTraceMessage is " & strTraceMessage ) End Sub </Script> <html> <head><title>Trace.aspx</title></head> <body> <h2>Testing Page Trace</h2> <% Trace.Warn( "Rendering page content!" ) %> </body> </html>
To enable runtime error messages to be displayed on a page, you need to use the Debug directive. To display errors in your ASP.NET page, include the following directive:
<%@ Page Debug="True" %>
When you include this directive, if an error is encountered when processing the page, the error is displayed. In most cases, you also can view the source code for the exact statement that generated the error.
To enable both tracing and debugging for a page, combine the directives like this (the order of Debug and Trace is not important):
<%@ Page Debug="True" Trace="True" %>
Import Directives
By default, only certain namespaces are automatically imported into an ASP.NET page. If you want to refer to a class that isn't a member of one of the default namespaces, then you must explicitly import the namespace of the class or you must use the fully qualified name of the class.
For example, suppose that you want to send an email from an ASP.NET page by using the Send method of the SmtpMail class. The SmtpMail class is contained in the System.Web.Mail namespace. This is not one of the default namespaces imported into an ASP.NET page.
The easiest way to use the SmtpMail class is to add an Import directive to your ASP.NET page to import the necessary namespace. The page in Listing 12 illustrates how to import the System.Web.Mail namespace and send an email message.
Listing 12ImportNamespace.aspx
<%@ Import Namespace="System.Web.Mail" %> <Script Runat="Server"> Sub Page_Load Dim mailMessage As SmtpMail mailMessage.Send( _ "bob@somewhere.com", _ "alice@somewhere.com", _ "Sending Mail!", _ "Hello!" ) End Sub </Script> <html> <head><title>ImportNamespace.aspx</title></head> <body> <h2>Email Sent!</h2> </body> </html>
The first line in Listing 12 contains an import directive. Without the import directive, the page would generate an error because it would not be able to resolve the SmtpMail class.
Instead of importing the System.Web.Mail namespace with the import directive, you could alternatively use its fully qualified name. In that case, you would declare an instance of the class like this:
Dim mailMessage As System.Web.Mail.SmtpMail
Code Declaration Blocks
A code declaration block contains all the application logic for your ASP.NET page and all the global variable declarations, subroutines, and functions. It must appear within a <Script Runat="Server"> tag.
Note that you can declare subroutines and functions only within a code declaration block. You receive an error if you attempt to define a function or subroutine in any other section of an ASP.NET page.
ASP Classic Note
In previous versions of ASP, you could declare a subroutine or function like this:
<% Sub mySub ... subroutine code End Sub %>
Don't try this declaration with ASP.NET because it generates an error. You must use the <Script> tag with ASP.NET like this:
<Script runat="Server"> Sub mySub ...subroutine code End Sub </Script>
The <Script Runat="Server"> tag accepts two optional attributes. First, you can specify the programming language to use within the <Script> tag by including a language attribute like this:
<Script Language="C#" Runat="Server">
If you don't specify a language, the language defaults to the one defined by the <%@ Page Language %> directive mentioned in the previous section. If no language is specified in a page, the language defaults to Visual Basic.
ASP Classic Note
In previous versions of Active Server Pages, you could include multiple languages in the same page by using the language attribute with the <Script> tag. This technique doesn't work with ASP.NET. You get an error if you attempt to use more than one language in the same page.
The second optional attribute of the <Script Runat="Server"> tag is SRC. You can use it to specify an external file that contains the contents of the code block. This is illustrated in Listings 13 and 14.
Listing 13ExternalFile.aspx
<Script Runat="Server" SRC="ApplicationLogic.aspx"/> <html> <head><title>ExternalFile.aspx</title></head> <body> <form Runat="Server"> <asp:label id="lblMessage" Runat="Server"/> <asp:Button Text="Click Here!" OnClick="Button_Click" Runat="Server"/> </form> </body> </html>
Listing 14ApplicationLogic.aspx
Sub Button_Click( s As Object, e As EventArgs ) lblMessage.Text = "Hello World!" End Sub
The page in Listing 13, ExternalFile.aspx, uses the SRC attribute to include the code in Listing 14, ApplicationLogic.aspx.
ASP.NET Controls
ASP.NET controls can be freely interspersed with the text and HTML content of a page. The only requirement is that the controls should appear within a <form Runat="Server"> tag. And, for certain tags such as <span Runat="Server"> and <ASP:Label Runat="Server"/>, this requirement can be ignored without any dire consequences.
One significant limitation of ASP.NET pages is that they can contain only one <form Runat="Server"> tag. This means that you cannot group ASP.NET into multiple forms on a page. If you try, you get an error.
Code Render Blocks
If you need to execute code within the HTML or text content of your ASP.NET page, you can do so within code render blocks. The two types of code render blocks are inline code and inline expressions. Inline code executes a statement or series of statements. This type of code begins with the characters <% and ends with the characters %>.
Inline expressions, on the other hand, display the value of a variable or method (this type of code is shorthand for Response.Write). Inline expressions begin with the characters <%= and end with the characters %>.
The ASP.NET page in Listing 15 illustrates how to use both inline code and inline expressions in an ASP.NET page.
Listing 15CodeRender.aspx
<Script Runat="Server"> Dim strSomeText As String Sub Page_Load strSomeText = "Hello!" End Sub </Script> <html> <head><title>CodeRender.aspx</title></head> <body> <form Runat="Server"> The value of strSomeText is: <%=strSomeText%> <p> <% strSomeText = "Goodbye!" %> The value of strSomeText is: <%=strSomeText%> </form> </body> </html>
Notice that you can use variables declared in the code declaration block within the code render block. However, the variable has to be declared with page scope. The variable could not, for example, be declared within the Page_Load subroutine.
Server-side Comments
You can add comments to your ASP.NET pages by using server-side comment blocks. The beginning of a server-side comment is marked with the characters <%-- and the end of the comment is marked with the characters --%>.
Server-side comments can be added to a page for the purposes of documentation. Note that you cannot see the contents of server-side comment tags, unlike normal HTML comment tags, by using the View Source command on your Web browser.
Server-side comments can also be useful when you're debugging an ASP.NET page. You can temporarily remove both ASP.NET controls and code render blocks from a page by surrounding these elements with server-side comments. The page in Listing 16 illustrates this use.
Listing 16ServerComments.aspx
<Script Runat="Server"> Dim strSomeText As String Sub Page_Load strSomeText = "Hello!" End Sub </Script> <html> <head><title>ServerComments.aspx</title></head> <body> <form Runat="Server"> <%-- This is inside the comments <asp:Label Text="hello!" Runat="Server" /> <%= strSomeText %> --%> This is outside the comments </form> </body> </html>
Server-side Include Directives
You can include a file in an ASP.NET page by using one of the two forms of the server-side include directive. If you want to include a file that is located in the same directory or in a subdirectory of the page including the file, you would use the following directive:
<!-- #INCLUDE file="includefile.aspx" -->
Alternatively, you can include a file by supplying the full virtual path. For example, if you have a subdirectory named myDirectory under the wwwroot directory, you can include a file from that directory like this:
<!-- #INCLUDE virtual="/myDirectory/includefile.aspx" -->
The include directive is executed before any of the code in a page. One implication is that you cannot use variables to specify the path to the file that you want to include. For example, the following directive would generate an error:
<!-- #INCLUDE file="<%=myVar%>" -->
TIP
Instead of working with include directives, consider working with user controls instead. User controls provide you with more flexibility. For example, you can create custom properties and methods with a user control.
Literal Text and HTML Tags
The final type of element that you can include in an ASP.NET page is HTML content. The static portion of your page is built with plain old HTML tags and text.
You might be surprised to discover that the HTML content in your ASP.NET page is compiled along with the rest of the elements. HTML content in a page is represented with the LiteralControl class. You can use the Text property of the LiteralControl class to manipulate the pure HTML portion of an ASP.NET page.
The HTML content contained in the page in Listing 17, for example, is reversed when it is displayed (see Figure 6). The Page_Load subroutine walks through all the controls in a page (including the LiteralControl) and reverses the value of the Text property of each control.
Listing 17Literal.aspx
<Script Runat="Server"> Sub Page_Load Dim litControl As LiteralControl For each litControl in Page.Controls litControl.Text = strReverse( litControl.Text ) Next End Sub </Script> <html> <head><title>Literal.aspx</title></head> <body> <b>This text is reversed</b> </body> </html>
Figure 6 Reversing the contents of a page.