ASP.NET pages are simply text files that have the .aspx file name extension, and can be placed on any web server equipped with ASP.NET.
Figure : The life cycle of the ASP.NET page
When a client requests an ASP.NET page, the web server passes the page to the ASP.NET runtime, a program that runs on the web server that’s responsible for reading the page and compiling it into a .NET class. This class is then used to produce the HTML that’s sent back to the user. Each subsequent request for this page avoids the compilation process: the .NET class can respond directly to the request, producing the page’s HTML and sending it to the client, until such time as the .aspx file changes. This process is illustrated in above Figure.
An ASP.NET page consists of the following elements:
Visual Basic LearningASP\VB\Hello.aspx(excerpt)
<%@ 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">
Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs)
myTimeLabel.Text = DateTime.Now.ToString()
End Sub
</script>
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title>Welcome to Build Your Own ASP.NET 3.5 Web Site!</title>
</head>
<body>
<form id="form1" runat="server">
<div>
<p>Hello there!</p>
<p>
The time is now:
<%-- Display the current date and time --%>
<asp:Label ID="myTimeLabel" runat="server" />
</p>
<p>
<%-- Declare the title as string and set it --%>
<% Dim Title As String = "This is generated by a code
render block."%>
<%= Title %>
</p>
</div>
</form>
</body>
</html>
Figure : The life cycle of the ASP.NET page
When a client requests an ASP.NET page, the web server passes the page to the ASP.NET runtime, a program that runs on the web server that’s responsible for reading the page and compiling it into a .NET class. This class is then used to produce the HTML that’s sent back to the user. Each subsequent request for this page avoids the compilation process: the .NET class can respond directly to the request, producing the page’s HTML and sending it to the client, until such time as the .aspx file changes. This process is illustrated in above Figure.
An ASP.NET page consists of the following elements:
- directives
- code declaration blocks
- code render blocks
- ASP.NET server controls
- server-side comments
- literal text and HTML tags
For the purpose of examining all the elements that can make up an ASP.NET page, we will not be using any code-behind files, as we did in Chapter 1. Code-behind files are useful at separating layout from code by breaking a web form into two files, but here all we’re interested in seeing is all the pieces of a web form in one place. This will make it easier to understand the structure of the web form.
The code below represents a version of the page you wrote in Chapter 1, which does not use a code-behind file. You’ll notice the server-side script code now resides in a script element:
The code below represents a version of the page you wrote in Chapter 1, which does not use a code-behind file. You’ll notice the server-side script code now resides in a script element:
Visual Basic LearningASP\VB\Hello.aspx(excerpt)
<%@ 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">
Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs)
myTimeLabel.Text = DateTime.Now.ToString()
End Sub
</script>
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title>Welcome to Build Your Own ASP.NET 3.5 Web Site!</title>
</head>
<body>
<form id="form1" runat="server">
<div>
<p>Hello there!</p>
<p>
The time is now:
<%-- Display the current date and time --%>
<asp:Label ID="myTimeLabel" runat="server" />
</p>
<p>
<%-- Declare the title as string and set it --%>
<% Dim Title As String = "This is generated by a code
render block."%>
<%= Title %>
</p>
</div>
</form>
</body>
</html>
C# LearningASP\CS\Hello.aspx(excerpt)
<%@ Page Language="C#" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<script runat="server">
protected void Page_Load(object sender, EventArgs e)
{
myTimeLabel.Text = DateTime.Now.ToString();
}
</script>
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title>Welcome to Build Your Own ASP.NET 3.5 Web Site!</title>
</head>
<body>
<form id="form1" runat="server">
<div>
<p>Hello there!</p>
<p>
The time is now:
<%-- Display the current date and time --%>
<asp:Label ID="myTimeLabel" runat="server" />
</p>
Build Your Own ASP.NET 3.5 Web Site Using C# & VB (www.sitepoint.com)
ASP.NET Basics 29
<p>
<%-- Declare the title as string and set it --%>
<% string Title = "This is generated by a code render block."; %>
<%= Title %>
</p>
</div>
</form>
</body>
</html>
If you like, you can save this piece of code in a file named Hello.aspx within the LearningASP\CS or LearningASP\VB directory you created in Chapter 1. You can open a new file in Visual Web Developer by selecting Website > Add New Item…. Use a Web Form template, and, since we are not using a code-behind file for this example, you need to deselect the Place code in a separate file checkbox. Alternatively, you can copy the file from the source code archive.
<%@ Page Language="C#" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<script runat="server">
protected void Page_Load(object sender, EventArgs e)
{
myTimeLabel.Text = DateTime.Now.ToString();
}
</script>
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title>Welcome to Build Your Own ASP.NET 3.5 Web Site!</title>
</head>
<body>
<form id="form1" runat="server">
<div>
<p>Hello there!</p>
<p>
The time is now:
<%-- Display the current date and time --%>
<asp:Label ID="myTimeLabel" runat="server" />
</p>
Build Your Own ASP.NET 3.5 Web Site Using C# & VB (www.sitepoint.com)
ASP.NET Basics 29
<p>
<%-- Declare the title as string and set it --%>
<% string Title = "This is generated by a code render block."; %>
<%= Title %>
</p>
</div>
</form>
</body>
</html>
If you like, you can save this piece of code in a file named Hello.aspx within the LearningASP\CS or LearningASP\VB directory you created in Chapter 1. You can open a new file in Visual Web Developer by selecting Website > Add New Item…. Use a Web Form template, and, since we are not using a code-behind file for this example, you need to deselect the Place code in a separate file checkbox. Alternatively, you can copy the file from the source code archive.
Executing the file (by hitting F5) should render the result shown in below Figure .
Figure : Sample page in action
Figure : Sample page in action
This ASP.NET page contains examples of all the above components (except server-side includes) that make up an ASP.NET page. You won’t often use every single element in a given page, but it’s important that you’re familiar with these elements, their purposes, and how and when it’s appropriate to use them.