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

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

elements with which your users can interact. There are three basic types of server

control: ASP.NET controls, HTML controls, and web user controls.

Usually, an ASP.NET control must reside within a

tag in order to function correctly. Controls offer the following advantages to ASP.NET

developers:

■ They give us the ability to access HTML elements easily from within our code:

we can change these elements’ characteristics, check their values, or even update

them dynamically from our server-side programming language of choice.

■ ASP.NET controls retain their properties thanks to a mechanism called
view

state
. We’ll be covering view state later in this chapter. For now, you need to

know that view state prevents users from losing the data they’ve entered into a

form once that form has been sent to the server for processing. When the response

comes back to the client, text box entries, drop-down list selections, and so on

are all retained through view state.

■ With ASP.NET controls, developers are able to separate a page’s presentational

elements (everything the user sees) from its application logic (the dynamic portions of the ASP.NET page), so that each can be maintained separately.

■ Many ASP.NET controls can be “bound” to the data sources from which they

will extract data for display with minimal (if any) coding effort.

ASP.NET is all about controls, so we’ll be discussing them in greater detail as we

move through this book. In particular,
Chapter 4 explains many of the controls that

ship with ASP.NET. For now, though, let’s continue our dissection of an ASP.NET

page.

Licensed to [email protected]

ASP.NET Basics

35

Server-side Comments

Server-side comments allow you to include within the page comments or notes that

won’t be processed by ASP.NET. Traditional HTML uses the character

sequences to delimit comments; any information included between these tags won’t

be displayed to the user. ASP.NET comments look very similar, but use the sequences

<%--and --%>.

Our ASP.NET example contains two server-side comment blocks, the first of which

is the following:

LearningASP\VB\Hello.aspx
(excerpt)

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

The difference between ASP.NET comments and HTML comments is that ASP.NET

comments are not sent to the client at all; HTML comments are, so they’re not suited

to commenting out ASP.NET code. Consider the following example:

C#


<%= Title %>

-->

Here, it looks as though a developer has attempted to use an HTML comment to

stop a code render block from being executed. Unfortunately, HTML comments will

only hide information from the browser, not the ASP.NET runtime. So in this case,

although we won’t see anything in the browser that represents these two lines, they

will be processed by ASP.NET, and the value of the variable Title will be sent to

the browser inside an HTML comment, as shown here:

-->

Licensed to [email protected]

36

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

The code could be modified to use server-side comments very simply:

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 can’t do without these elements—after all, HTML allows the display of the information in your ASP.NET controls and code in a way that’s suitable for users and their browsers. Let’s take a look at the literal text and HTML tags that were used to

produce the display in the Visual Basic version of our sample page (the text and

HTML in the C# version is identical):

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]

ASP.NET Basics

37

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 %>






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.

By now, you should have a clearer understanding of the structure of an ASP.NET

page. As you work through the examples in this book, you’ll begin to realize that,

in many cases, you won’t need to use all of these elements. For the most part, your

development will be modularized within code-behind files or code declaration

blocks, and all of the dynamic portions of your pages will be contained within code

render blocks or controls located inside a

tag.

In the following sections, we’ll explore view state, discuss working with directives,

and shine a little light on the languages that can be used within ASP.NET.

View State

ASP.NET controls automatically retain their data when a page is sent to the server

in response to an event (such as a user clicking a button). Microsoft calls this persistence of data
view state
. In the past, developers would have had to resort to hacks to have the application remember the item a user had selected in a drop-down menu,

or store the content entered into a text box; typically, these hacks would have relied

on hidden form fields.

Licensed to [email protected]

38

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

This is no longer the case. Once they’re submitted to the server for processing,

ASP.NET pages automatically retain all the information contained in text boxes and

drop-down lists, as well as radio button and checkbox selections. They even keep

track of dynamically generated tags, controls, and text. Consider the following code

written in the “ancient” ASP (not ASP.NET!) framework:

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



Sample Page using VBScript





value="Click Me" />

<%

If Request.Form("nameTextBox") <> "" Then

Response.Write(Request.Form("nameTextBox"))

End If

%>




Loading this page through an ASP-enabled web server (such as IIS) would reveal

that the view state is not automatically preserved. When the user submits the form,

the information that was typed into the text box is cleared, although it’s still available

in the Request.Form("nameTextBox") property. The equivalent page in ASP.NET

demonstrates this data persistence using view state:

Visual Basic

LearningASP\VB\ViewState.aspx

<%@ Page Language="VB" %>

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




View State Example






Text="Click Me" OnClick="Click" />






C#

LearningASP\CS\ViewState.aspx

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

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




View State Example






Text="Click Me" OnClick="Click" />



Licensed to [email protected]

40

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




In this case, the code uses ASP.NET controls with the runat="server" attribute.

As you can see in Figure 2.3
, the text from the box appears on the page when the button is clicked, but also notice that the data remains in the text box! The data in

this example is preserved by view state.

Figure 2.3. ASP.NET maintaining the state of the controls

You can see the benefits of view state already. But where’s all that information

stored?

ASP.NET pages maintain view state by encrypting the data within a hidden form

field. View the source of the page after you’ve submitted the form, and look for the

following code:

value="/wEPDwUKLTEwNDY1Nzg0MQ9…0fMCR+FN5P6v5pkTQwNEl5xhBk" />

This is a standard HTML hidden form field. All information that’s relevant to the

view state of the page is stored within this hidden form field as an encrypted string.

View state is enabled for every page by default. If you don’t intend to use view state,

you can turn it off, which will result in a slight performance gain in your pages. To

do this, set the EnableViewState property of the Page directive to false:

<%@ Page
EnableViewState="False"
%>

Licensed to [email protected]

Other books

When Love Hurts by Shaquanda Dalton
Ironbark by Jonsberg, Barry
The Carrier by Sophie Hannah
Brink of Chaos by Tim LaHaye