C#
<%--
<% string Title = "This is generated by a code render block."; %>
<%= Title %>
--%>
The ASP.NET runtime will ignore the contents of this comment, and the value
of the Title variable will not be output.
Literal Text and HTML Tags
The final elements of an ASP.NET page are plain old text and HTML. Generally,
you cant do without these elementsafter all, HTML allows the display of the
information in your ASP.NET controls and code in a way thats suitable for users.
Lets take a look at the literal text and HTML tags that were used to produce the
display in Figure 2.2:
Visual Basic File: Hello.aspx (excerpt)
<%@ Page Language="VB" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html>
<head>
<title>Sample Page</title>
<script runat="server">
Sub Page_Load()
messageLabel.Text = "Hello World!"
End Sub
</script>
</head>
<body>
<form runat="server">
<p>
<asp:Label id="messageLabel" 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>
</form>
</body>
</html>
42
Chapter 2: ASP.NET Basics
C# File: Hello.aspx (excerpt)
<%@ Page Language="C#" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html>
<head>
<title>Sample Page</title>
<script runat="server">
void Page_Load()
{
messageLabel.Text = "Hello World";
}
</script>
</head>
<body>
<form runat="server">
<p>
<asp:Label id="messageLabel" runat="server" />
</p>
<p>
<%-- Declare the title as string and set it --%>
<% string Title = "This is generated by a code render " +
"block."; %>
<%= Title %>
</p>
</form>
</body>
</html>
The bold code above highlights the fact that literal text and HTML tags provide
the structure for presenting our dynamic data. Without these elements, this page
would have no format, and the browser would be unable to understand it.
You now have a clearer understanding of the structure of an ASP.NET page. As
you work through the examples in this book, youll begin to realize that, in many
cases, you wont need to use all of these elements. For the most part, your devel-
opment will be modularized within code declaration blocks, and all of the dynamic
portions of your pages will be contained within code render blocks or controls
located inside a <form runat="server">> tag.
In the following sections, well explore view state, discuss working with directives,
and shine a little light on the languages that can be used within ASP.NET.
43
Literal Text and HTML Tags

Get Build Your Own ASP.NET 2.0 Web Site Using C# & VB, Second Edition now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.