Build Your Own ASP.NET 3.5 Website Using C# & VB (11 page)

Read Build Your Own ASP.NET 3.5 Website Using C# & VB Online

Authors: Cristian Darie,Zak Ruvalcaba,Wyatt Barnett

Tags: #C♯ (Computer program language), #Active server pages, #Programming Languages, #C#, #Web Page Design, #Computers, #Web site development, #internet programming, #General, #C? (Computer program language), #Internet, #Visual BASIC, #Microsoft Visual BASIC, #Application Development, #Microsoft .NET Framework

BOOK: Build Your Own ASP.NET 3.5 Website Using C# & VB
7.81Mb size Format: txt, pdf, ePub

between these two languages, and form a clear idea of the power that they provide

for/ those creating ASP.NET applications.

So, what exactly makes up an ASP.NET page? The next few sections will give you

an in-depth understanding of the constructs of a typical ASP.NET page.

ASP.NET Page Structure

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 2.1. 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,

Licensed to [email protected]

ASP.NET Basics

27

producing the page’s HTML and sending it to the client, until such time as the
.aspx

file changes. This process is illustrated in Figure 2.1
.

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:

Visual Basic

LearningASP\VB\Hello.aspx
(excerpt)

<%@ Page Language="VB" %>

"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">




Welcome to Build Your Own ASP.NET 3.5 Web Site!




Licensed to [email protected]

28

Build Your Own ASP.NET 3.5 Web Site Using C# & VB


Hello there!



The time is now:

<%--Display the current date and time --%>




<%--Declare the title as string and set it --%>

<% Dim Title As String = "This is generated by a code

render block."%>

<%= Title %>






C#

LearningASP\CS\Hello.aspx
(excerpt)

<%@ Page Language="C#" %>

"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">




Welcome to Build Your Own ASP.NET 3.5 Web Site!





Hello there!



The time is now:

<%--Display the current date and time --%>


Licensed to [email protected]

ASP.NET Basics

29



<%--Declare the title as string and set it --%>

<% string Title = "This is generated by a code render

block."; %>

<%= Title %>






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. Y
ou 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 Figure 2.2.

Figure 2.2. Sample page in action

This ASP.NET page contains examples of all the above components (except serverside 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.

Licensed to [email protected]

30

Build Your Own ASP.NET 3.5 Web Site Using C# & VB

Directives

The directives section is one of the most important parts of an ASP.NET page. Directives control how a page is compiled, specify how a page is cached by web browsers, aid debugging (error-fixing), and allow you to import classes to use

within your page’s code. Each directive starts with <%@. This is followed by the

directive name, plus any attributes and their corresponding values. The directive

then ends with %>.

There are many directives that you can use within your pages, and we’ll discuss

them in greater detail later. For the moment, however, know that the Import and

Page directives are the most useful for ASP.NET development. Looking at our sample

ASP.NET page,
Hello.aspx
, we can see that a Page directive was used at the top of

the page like so:

Visual Basic

LearningASP\VB\Hello.aspx
(excerpt)

<%@ Page Language="VB" %>

C#

LearningASP\CS\Hello.aspx
(excerpt)

<%@ Page Language="C#" %>

In this case, the Page directive specifies the language that’s to be used for the application logic by setting the Language attribute. The value provided for this attribute, which appears in quotes, specifies that we’re using either VB or C#. A whole range

of different directives is available; we’ll see a few more later in this chapter.

ASP.NET directives can appear anywhere on a page, but they’re commonly included

at its very beginning.

Code Declaration Blocks

In
Chapter 3, we
’ll talk more about code-behind pages and how they let us separate our application logic from an ASP.NET page’s HTML. However, if you’re 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

Licensed to [email protected]

ASP.NET Basics

31

variables, subroutines, functions, and more. In our sample page, we’ve placed the

code inside

C#

LearningASP\CS\Hello.aspx
(excerpt)


Comments in VB and C# Code

Both of these code snippets contain comments—explanatory text that will be ignored

by ASP.NET, but which serves to describe to us how the code works.

In VB code, a single quote or apostrophe (') indicates that the remainder of the line

is to be ignored as a comment, while in C# code, two slashes (//) achieve the same

end:

Visual Basic

LearningASP\VB\Hello.aspx
(excerpt)

'set the label text to the current time

C#

LearningASP\CS\Hello.aspx
(excerpt)

//set the label text to the current time

Licensed to [email protected]

32

Build Your Own ASP.NET 3.5 Web Site Using C# & VB

C# code also lets us span a comment over multiple lines if we begin it with /* and

end it with */, as in this example:

/*set the label text

to the current time */