C# File: Hello.aspx (excerpt)
<%@ Page Language="C#" %>
In this case, the Page directive specifies the language thats to be used for the
application logic by setting the Language attribute. The value provided for this
attribute, which appears in quotes, specifies that were using either VB or C#. A
whole range of different directives is available; well see a few more later in this
chapter.
Unlike ASP, ASP.NET directives can appear anywhere on a page, but theyre
commonly included at its very beginning.
Code Declaration Blocks
In Chapter 3, well talk about code-behind pages and how they let us separate
our application logic from an ASP.NET pages HTML. However, if youre not
working with code-behind pages, you must use code declaration blocks to contain
all the application logic of your ASP.NET page. This application logic defines
variables, subroutines, functions, and more. In our page, weve placed the code
inside <script> tags, like so:
Visual Basic
<script runat="server">
Sub mySub()
' Code here
End Sub
</script>
Here, the tags enclose VB code, but it could just as easily be C#:
C#
<script runat="server">
void mySub()
{
// Code here
}
</script>
Comments in VB and C# Code
Both of these code snippets contain commentsexplanatory text that will be ig-
nored by ASP.NET, but which serves to describe to us how the code works.
37
Code Declaration Blocks
In VB code, a single quote or apostrophe (') indicates that the remainder of the
line is to be ignored as a comment.
In C# code, two slashes (//) achieve the same end. C# code also lets us span a
comment over multiple lines if we begin it with /* and end it with */, as in this
example:
C#
<script runat="server">
void mySub()
{
/* Multi-line
comment */
}
</script>
Before .NET emerged, ASP also supported such script tags using a runat="serv-
er" attribute. However, they could only ever contain VBScript and, for a variety
of reasons, they failed to find favor among developers.
Code declaration blocks are generally placed inside the head of your ASP.NET
page. The sample ASP.NET page shown in Figure 2.2, for instance, contains the
following code declaration block:
Visual Basic File: Hello.aspx (excerpt)
<script runat="server">
Sub Page_Load()
messageLabel.Text = "Hello World"
End Sub
</script>
Perhaps you can work out what the equivalent C# code would be:
C# File: Hello.aspx (excerpt)
<script runat="server">
void Page_Load()
{
messageLabel.Text = "Hello World";
}
</script>
The <script runat="server"> tag also accepts two other attributes. We can
set the language thats used in this code declaration block via the language at-
tribute:
38
Chapter 2: ASP.NET Basics

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.