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
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#" %>
TIP : 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. 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.
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Trace.aspx.cs" Inherits="Chapter1_Trace" Trace="true" Debug="True" %>
<script runat="server" >
protected void Page_Load(object sender, EventArgs e)
{
string strTraceMessage;
Trace.Warn("Page_Load event executing!");
strTraceMessage = "Hello World!";
Trace.Write("The value of strTraceMessage is " + strTraceMessage);
}
</script>
<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server"><title>Trace</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.
II 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 e-mail 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.
<%@ Import Namespace="System.Web.Mail" %>
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.
<Script runat="Server">
int mySub
{
...subroutine code
}
</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 C#.
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.
<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>
protected void Button_Click(object sender, EventArgs e)
{
if (sender.Equals(btnHello))
lblMessage.Text = "Hello!";
else
lblMessage.Text = "Goodbye!";
}
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 %>.
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="CodeRender.aspx.cs" Inherits="Chapter1_CodeRender" %>
<script runat="server" >
string strSomeText;
protected void Page_Load(object sender, EventArgs e)
{
strSomeText = "Hello!";
}
</script>
<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
<title>CodeRender</title>
</head>
<body>
<form id="form1" runat="server">
<div>
The value of strSomeText is: <%=strSomeText%>
<p>
<% strSomeText = "Goodbye!"; %>
The value of strSomeText is: <%=strSomeText%>
</div>
</form>
</body>
</html>
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.
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" -->
No comments:
Post a Comment