All-In-One / MCAD/MCSD Visual C# .NET Certification All-in-One Exam Guide / Rempel & Lind / 222443-6 / Chapter
Composite Default screen
11
P:\010Comp\All-in-1\443-6\ch11.vp
Monday, August 26, 2002 11:44:09 AM
Color profile: Generic CMYK printer profile
Composite Default screen All-In-One / MCAD/MCSD Visual C# .NET Certification All-in-One Exam Guide / Rempel & Lind / 222443-6 /
Blind Folio 2
P:\010Comp\All-in-1\443-6\ch11.vp
Monday, August 26, 2002 11:44:09 AM
Color profile: Generic CMYK printer profile
All-In-One / MCAD/MCSD Visual C# .NET Certification All-in-One Exam Guide / Rempel & Lind / 222443-6 / Chapter
Composite Default screen
11
11
In this chapter, you will
• Discover the Internet standards
• Delve into the protocols used on the Internet
• Explore XML
• Master the Internet architecture
• Learn about the ASP.NET object architecture
Active Server Pages (ASP) was released in 1996 by Microsoft to act as the interface
for web application development centered on their Internet Information Server (IIS).
The release of ASP and IIS 3.0 brought a new era for web developers around the world.
The ASP object model was programmed with scripted languages like VBScript and
ECMAScript (JavaScript).
Scripting of web sites introduced a very convoluted application that was hard to
maintain and to scale—this was one of the potential problems in the ASP architecture.
With the release of the ASP.NET object model, Microsoft has based their web server
architecture on the .NET Framework, which means we can program web applications
with any mixture of the .NET languages (C#, C++, J#, JavaScript, and VB .NET from
Microsoft), including the third-party Cobol.NET that will be released for the .NET
Framework.
In this chapter, you will be introduced to the basics of ASP.NET and we will define
the acronyms and look at the technology that will be used throughout this part of the
book. We will also create a first web application to display “Hello World!” in the
browser. So without any more delays we’ll start the journey towards ASP.NET with a
visit to the Internet.
P:\010Comp\All-in-1\443-6\ch11.vp
Monday, August 26, 2002 11:44:09 AM
Color profile: Generic CMYK printer profile
Composite Default All-In-One
screen / MCAD/MCSD Visual C# .NET Certification All-in-One Exam Guide / Rempel & Lind / 222443-6 / Chapter
11
P:\010Comp\All-in-1\443-6\ch11.vp
Monday, August 26, 2002 11:44:09 AM
Color profile: Generic CMYK printer profile
All-In-One / MCAD/MCSD Visual C# .NET Certification All-in-One Exam Guide / Rempel & Lind / 222443-6 / Chapter
Composite Default screen
11
The key parts of the list can be found in steps 1 through 3—they are called the
PART III
three-way handshake, involving calling a number, identifying the other party, and iden-
tifying yourself. The TCP protocol does exactly that. As part of the three-way handshake,
the two computers will configure the parameters they will use to acknowledge data
receipt.
All Web-related network traffic uses TCP as its transport protocol.
Ports
When you connect to a computer on the Internet, you supply the address of the com-
puter you’re trying to connect with. This is the same as when you call a courier to deliver
a package to someone in an office, and you give the courier the address of the office.
Suppose you want to send a letter via a courier to Greg at 1190 George Street in Mimico;
this is an office building with eight floors. If that is all the information we give our in-
trepid courier, he will have to randomly go through the building looking for Greg.
Computers are analogous to business offices; the computer has many applications
(employees) that perform different tasks. If we connected using only the address of the
computer, we would have to search through all the applications looking for the one we
were trying to connect to, which is very inefficient. The solution is to give each applica-
tion a number (like an employee cubicle number) that can be used to identify the appli-
cation. When we want to connect, we pass this number along, making the process of
finding the application on the computer seamless.
These numbers are commonly called ports, and there are 65,536 possible port num-
bers. To make the port numbers more functional, the first 1,024 ports are defined for
P:\010Comp\All-in-1\443-6\ch11.vp
Monday, August 26, 2002 11:44:10 AM
Color profile: Generic CMYK printer profile
Composite Default All-In-One
screen / MCAD/MCSD Visual C# .NET Certification All-in-One Exam Guide / Rempel & Lind / 222443-6 / Chapter
11
HTML
Hypertext Markup Language (HTML) saw the light of day as part of a much larger stan-
dard (Standard Generalized Markup Language, SGML) in 1986. HTML was not used for
web pages until the early 1990s but has become a standard by itself since then.
The HTML language defines how various elements on a web page are to be inter-
preted by the web browser and rendered (displayed). A web browser does not have to
understand all the possible elements that are defined in the standard, but if the browser
finds an unknown element, it must ignore it. This rule makes it possible for different
versions of HTML to be used in different documents—if a new element is added to the
standard – all older browsers will ignore the element, while browsers designed for the
new version will properly display the element.
Pages that are to be sent to a browser follow a predefined layout, as you can see in the
following examples (and in Table 11-1). Elements in HTML consist of an opening and a
closing tag surrounding the element. For example, a TITLE tag would be entered like
this:
The element tags are enclosed with angle brackets (< and >), with the slash (/) indicat-
ing that the second of the two is a closing tag.
The following HTML document will display the text “Hello HTML World!” in any
browser:
<HTML>
<HEAD>
<TITLE>The Hello World Page</TITLE>
</HEAD>
<BODY>
<H1>Hello HTML World!</H1>
</BODY>
</HTML>
HTTP
The Hypertext Transfer Protocol is at the heart of the Internet. This is the protocol that
gives us the ability to send information seamlessly between web browsers and web serv-
ers . HTTP defines how a browser requests a web page and how the server responds to
that request.
The standard defines two messages that are used for the communication. The GET
message is what the browser uses to request a page, and the POST message is what the
server uses to respond to the browser. On the surface, this looks straightforward—the
P:\010Comp\All-in-1\443-6\ch11.vp
Monday, August 26, 2002 11:44:10 AM
Color profile: Generic CMYK printer profile
All-In-One / MCAD/MCSD Visual C# .NET Certification All-in-One Exam Guide / Rempel & Lind / 222443-6 / Chapter
Composite Default screen
11
PART III
<H1> The H1 element identifies the first heading level, and it
specifies that the text in the element should be displayed
with the largest, boldest font possible. The HTML
standard defines headings from H1 through to H6.
browser sends a GET message and the server sends a POST message. As you will see,
however, the browser can also send POST messages that are interpreted as requests by
the server. .
An HTTP GET message is a maximum of 1,024 characters in length, and it contains
the name and path of the document requested on the server. In addition, it should in-
clude the browser’s model and version, operating system version, screen resolution,
username (“anonymous” if not known), and a host of other information about the
browser environment.
The POST message has a header that is a maximum of 1,024 characters in length, plus
an unlimited body size. POST messages are used to return requested items from the
server to the browser, and are also used by FORM elements that are used by the browser
to group fields together to return information to the server.
EXAM TIP The authors of the exams assume that you have a firm knowledge
of HTML, specially the TABLE and FORM elements.
The header in a POST message is virtually the same as for the GET message. The POST
message can be used to send any type of data in the body of the message. The type pa-
rameter in the header specifies what the data is so the receiver can handle it properly.
XML
When it comes to standards, they don’t come any cooler than XML. XML is based on the
same standard that HTML is (SGML), it uses the familiar HTML syntax. You will have
encountered XML in Chapter 8 already, the following discussion serves as a refresher.
P:\010Comp\All-in-1\443-6\ch11.vp
Monday, August 26, 2002 11:44:10 AM
Color profile: Generic CMYK printer profile
Composite Default All-In-One
screen / MCAD/MCSD Visual C# .NET Certification All-in-One Exam Guide / Rempel & Lind / 222443-6 / Chapter
11
Given that the only requirement of a well-formed XML document is that it must fol-
low these six rules, we could create many different documents to describe the same in-
formation. This would cause problems for businesses if customers sent in their XML
orders in various formats. The solution to this quandary as you saw in Chapter 8 – it is
to use a grammar or schema. Using an XML schema, we can guarantee that the XML
documents all have the same structure. The XML schema defines the layout of the XML doc-
ument, the names of the elements, the data types for those elements, and how the ele-
ments are related to each other.
EXAM TIP The use of XML in virtually all configuration files means that you
will be presented with XML documents in a large number of questions. Make
sure you are familiar with how to read XML documents.
Internet Architecture
The Internet is a client/server architecture, where the client (browser) requests services
from the server (web server). This architecture is at the center of the popularity explo-
sion we have seen for the Internet.It is the place that we get our news, buy our groceries,
and plan our travel, just to mention some of the many things that are possible because
the Internet is built around the Client/Server model.
P:\010Comp\All-in-1\443-6\ch11.vp
Monday, August 26, 2002 11:44:10 AM
Color profile: Generic CMYK printer profile
All-In-One
Composite Default screen All-In-One
/ MCAD/MCSD
/ MCSD Visual
Visual
C#C#
.NET
.NET
Certification
Certification
All-in-One
All-in-One
Exam
Exam
Guide
Guide
/ Rempel
/ Rempel
& Lind
& Lind
/ 222443-6
/ 222443-6
/ Chapter
/ Chapter
11
11
PART III
ple languages. Because we do not know what language the client uses, we will use as
many languages as we know. By writing the following HTML code and saving it as
Greeting.htm, you will create a page that produces the output shown in Figure 11-1.
<html>
<head>
<title>The Greeting Page</title>
</head>
<body>
<h1>Welcome</h1>
<h1>Välkommen</h1>
<h1>Willkommen</h1>
<h1>Bienvenu</h1>
<h1>Bienvenido</h1>
<hr>
<br>
<p>Welcome to our International page
</body>
</html>
Every time this page is loaded in the client’s browser, it will display the same informa-
tion; it is static. The content of the page was determined before the request for the page
was made—it was determined when the page was saved to the disk.
So how does the HTML page get to the client’s browser from the hard drive of the web
server? The following steps outline the process, and it is shown in Figure 11-2:
1. The user requests the page through the browser, and the request is sent to
the web server.
2. The web server finds the page on its hard drive, and packages it up as a
response.
3. The web server returns the response to the client browser.
4. The browser receives the response, processes the HTML code, and displays
the page.
P:\010Comp\All-in-1\443-6\ch11.vp
Monday, August 26, 2002 11:44:10 AM
Color profile: Generic CMYK printer profile
Composite Default All-In-One
screen / MCAD/MCSD Visual C# .NET Certification All-in-One Exam Guide / Rempel & Lind / 222443-6 / Chapter
11
The static page was the model for many web sites, some of which were very innova-
tive and provided changing content—these interesting web sites resulted from the hard
work of the webmasters who managed the pages and updated them on an ongoing ba-
sis. One problem with static pages is just that; it takes a large effort to keep the pages up-
dated as information changes.
Another challenge of static pages is that they give us no way of personalizing the page
based on the users’ preferences, or their previous browsing history. The static page is
written before the client requests it, so there is no possibility of making changes based
on the actual conditions when the page is used.
P:\010Comp\All-in-1\443-6\ch11.vp
Monday, August 26, 2002 11:44:10 AM
Color profile: Generic CMYK printer profile
All-In-One / MCAD/MCSD Visual C# .NET Certification All-in-One Exam Guide / Rempel & Lind / 222443-6 / Chapter
Composite Default screen
11
PART III
Figure 11-2 The process of serving a static web page
All browsers can display the HTML code that makes up a static web page. However,
with static pages there is no way to secure the code, so it will always be available to any-
one who wants to see it and copy it.
There are two ways to work around the limitations of the static web page—we will ex-
plore both techniques in the following sections.
1. The user requests the page through the browser, and the request is sent to the
web server.
P:\010Comp\All-in-1\443-6\ch11.vp
Monday, August 26, 2002 11:44:11 AM
Color profile: Generic CMYK printer profile
Composite Default All-In-One
screen / MCAD/MCSD Visual C# .NET Certification All-in-One Exam Guide / Rempel & Lind / 222443-6 / Chapter
11
2. The web server finds the page on its hard drive, plus the server might also have
to locate the file with the scripts, and it packages up the response.
3. The web server returns the response to the client browser.
4. The browser receives and executes the scrip instructions and renders (returns)
HTML for display.
5. The browser processes the HTML code and displays the page.
While the client-side model was very innovative a number of years ago, there is a
problem with it in that different browser vendors have totally different support for the
scripting and the architecture of the browser (known as the DOM—Document Object
Model). In addition, the scripts must be sent down to the client to be executed, result-
ing in a larger page size and longer download times. And if the script needs to access
server resources, such as databases, it is the client that must connect and manipulate
the resource, which again is not very efficient. There is possibly an even more serious
problem in that the script code is clearly available in the client’s browser, creating a se-
curity problem.
The result of these limitations is that the client-side model has fallen out of favor with
web site developers. The current web site architecture is to use the server to run scripts to
create the HTML that the browser will then display, as we will see in the next section.
P:\010Comp\All-in-1\443-6\ch11.vp
Monday, August 26, 2002 11:44:11 AM
Color profile: Generic CMYK printer profile
All-In-One / MCAD/MCSD Visual C# .NET Certification All-in-One Exam Guide / Rempel & Lind / 222443-6 / Chapter
Composite Default screen
11
1. The user requests the page through the browser, and the request is sent to
the web server.
2. The web server finds the page on its hard drive.
3. The web server routes the file with processing instructions to a part of the
server that will perform the processing.
4. The web server returns the HTML response to the client browser.
PART III
5. The browser receives the response, processes the HTML code, and displays
the page.
<html>
<head>
<title>The Greeting Page</title>
</head>
<body>
<h1>Welcome</h1>
<h1>Välkommen</h1>
<h1>Willkommen</h1>
<h1>Bienvenu</h1>
<h1>Bienvenido</h1>
<hr>
<br>
<p>Welcome to our International page
<p>The time here in the Dynamic WebLand is: <%=Time()%>
</body>
</html>
The file is saved on the web server using the name Greeting.asp. The <% and %>
tag delimiters tell the server process that the content of the element is a script to be exe-
cuted. The =Time() script returns the current time on the server. Saving the file with an
extension of .asp tells the server to send the file to the process that handles .asp files
rather than to return the file without processing.
P:\010Comp\All-in-1\443-6\ch11.vp
Monday, August 26, 2002 11:44:11 AM
Color profile: Generic CMYK printer profile
Composite Default All-In-One
screen / MCAD/MCSD Visual C# .NET Certification All-in-One Exam Guide / Rempel & Lind / 222443-6 / Chapter
11
Figure 11-4 The process of serving a dynamic web page from the server.
The preceding example used the ASP scripting model, and we will get back to this ex-
ample later in the chapter to see how we can develop the page using ASP.NET. First,
though, we must look at the technologies in use.
Client-Side Technologies
There are a number of technologies in use on the client side that we need to look at. The
client-side technologies were developed to solve problems and, in some cases, to be
“different” than a competitor.
VBScript
Microsoft developed a scripted version of the popular Visual Basic language, VBScript,
which was introduced in Internet Explorer 3.0. The VBScript language is not case sensi-
tive, making it the language of choice for VB developers. It is a direct competitor to
JavaScript. VBScript has one serious drawback: it is only available in Microsoft browsers,
making it of limited use on the client.
P:\010Comp\All-in-1\443-6\ch11.vp
Monday, August 26, 2002 11:44:11 AM
Color profile: Generic CMYK printer profile
All-In-One / MCAD/MCSD Visual C# .NET Certification All-in-One Exam Guide / Rempel & Lind / 222443-6 / Chapter
Composite Default screen
11
Java Applets
Java is a name that describes a number of different technologies; it is among others a
language, a runtime environment, and a deployment environment. Java was originally
developed as a language that would solve some of the problems found in developing
PART III
applications in C++. Java is firmly in the C family of languages, but is also an object ori-
ented language. Java applets are small components that require a container to execute,
and the most common container is a web browser.
Both Microsoft Internet Explorer and Netscape Navigator implement the runtime en-
vironment for Java applets (Java Virtual Machine—JVM), giving you a wide range of
browsers that support this technology. Java has not become the cure-all technology that
it was expected to be by some industry experts, but it is still a language that has growing
support in the web-development community.
ActiveX Controls
The ActiveX control is a Microsoft technology that is very much dependent on the
Windows platform for its use. The ActiveX controls are usually written in C++, but can
also be developed using VB. The technology is limited to use in Internet Explorer and so
has gone out of vogue. Netscape Navigator can use ActiveX controls with an add-on, but
if we have to make the user install specific software to work with our site, there will be a
large number of users who will not install the add-on and therefore will not be able
to take advantage of our pages. For these reasons, ActiveX controls have fallen into the
legacy category.
Server-Side Technologies
The server-side technologies are add-ons to the server software. They process the re-
quested file before it is returned to the client, and they produce HTML as their output.
The aim is to be able to dynamically build the HTML responses using a technology that
does not need to be installed on the client, making the design as browser neutral as pos-
sible. The first server-side technology was CGI, and the most current is ASP.NET.
CGI
The Common Gateway Interface (CGI) has been used on web sites for quite some time;
it is one of the most widely used technologies for creating dynamic content sites. CGI
P:\010Comp\All-in-1\443-6\ch11.vp
Monday, August 26, 2002 11:44:12 AM
Color profile: Generic CMYK printer profile
Composite Default All-In-One
screen / MCAD/MCSD Visual C# .NET Certification All-in-One Exam Guide / Rempel & Lind / 222443-6 / Chapter
11
ASP
Active Server Pages (ASP) are built using either VBScript or JavaScript, and they have ac-
cess to the same services as a Windows application, including ADO (ActiveX Data Ob-
jects) for database access, SMTP (Simple Mail Transfer Protocol) for e-mail, and the
entire COM (Component Object Model) structure used in the Windows environment.
ASP is now a legacy technology and is commonly called classic ASP.
ASP is implemented through a dynamic-link library (asp.dll) that is called by the
IIS server when an .asp page is requested from the server. When ASP was released, it
made it possible for developers in the Windows family of servers to start developing dy-
namic web sites, and although the ASP technology has some very good features, it also
has some that make developers cringe. Speed is one—every time a page is loaded, the
script must be processed through the asp.dll component. Being limited to scripted
languages for development is another problem.
JSP
Java Server Pages (JSP) is a technology that lets you leverage your Java knowledge by
combining HTML and Java code to build a dynamic response from the server. The JSP
technology is available for almost all web servers, including IIS, giving the panacea of
platform independence for the code.
PHP
PHP is an acronym with two meanings—the original meaning was Personal Home
Pages, and the acronym currently translates to PHP Hypertext Preprocessor (that
sought-after acronym within an acronym). PHP is similar to ASP in architecture and is
built through a scripting language reminiscent of C and Perl. PHP is open source and
is available for many different platforms.
P:\010Comp\All-in-1\443-6\ch11.vp
Monday, August 26, 2002 11:44:12 AM
Color profile: Generic CMYK printer profile
All-In-One / MCAD/MCSD Visual C# .NET Certification All-in-One Exam Guide / Rempel & Lind / 222443-6 / Chapter
Composite Default screen
11
ASP.NET
Finally! We have now made it to the topic of this part of the book, ASP.NET, Microsoft’s
newest and most powerful web-server technology designed to create dynamic server-side
pages. ASP.NET is implemented as a server component (aspnet_isapi.dll) that uses
the .NET Framework to perform all the processing for the dynamic page. The rest of Part
III will deal with the specifics of ASP.NET, starting in the next section, where you will dis-
cover what ASP.NET is and start learning how to control it.
PART III
ASP.NET
ASP.NET is distributed by Microsoft as part of the .NET Framework – the .NET Frame-
work is made available as a free download from the Microsoft download site. In addi-
tion to ASP.NET, the .NET Framework also contains the runtime environment for all the
.NET languages (Visual C# .NET, Visual C++ .NET, Visual Basic .NET, and JScript .NET).
This runtime environment includes the compilers as well as the base class library.
In our investigation of this environment, we will start with a quick “Hello Web
World!” web application.
Step two is to create a directory to store your web pages. This new directory will be lo-
cated in the default root directory for IIS, which is the directory you will access when you
P:\010Comp\All-in-1\443-6\ch11.vp
Monday, August 26, 2002 11:44:12 AM
Color profile: Generic CMYK printer profile
Composite Default All-In-One
screen / MCAD/MCSD Visual C# .NET Certification All-in-One Exam Guide / Rempel & Lind / 222443-6 / Chapter
11
NOTE We will refer to the “Visual Studio .NET Command Prompt” window
as the command prompt from now on.
Now that we have a directory, we can create our first ASP.NET program. At the
command prompt, change the directory to the one you created (csw). Then, at the
command prompt open a new file with the Notepad editor by entering notepad
HelloOne.aspx, and confirm that Notepad should create a new file.
Now you can enter the following ASP.NET code in the editor, the line numbers are
for reference only, they are not to be entered in the program:
1. <script language="c#" runat="server">
2. void Page_Load()
3. {
4. lblTime.Text = "Hello Web World! The time here is " +
5. DateTime.Now.Hour.ToString() + ":" +
6. DateTime.Now.Minute.ToString();
7. }
8. </script>
9. <html>
10. <head><title>The Punctual Web Server</title></head>
11. <body>
12. <h1>Welcome</h1>
13. Here in ASP.NET Server Land the current time is:
14. <asp:label id="lblTime" runat="server" />
15. </body>
16. </html>
Save the page from Notepad, and view the page in Internet Explorer by typing the ad-
dress into the address bar: http://localhost/csw/HelloOne.aspx. The code produces the
display in Figure 11-5.
The first code line tells the ASP.NET environment that the program in this file should
execute (runat) at the server, and that the language of the file is C#:
<script language="c#" runat="server">
The code in line 2 is the code that will run when the page loads.
P:\010Comp\All-in-1\443-6\ch11.vp
Monday, August 26, 2002 11:44:12 AM
Color profile: Generic CMYK printer profile
All-In-One / MCAD/MCSD Visual C# .NET Certification All-in-One Exam Guide / Rempel & Lind / 222443-6 / Chapter
Composite Default screen
11
PART III
Lines 4, 5, and 6 are actually the same line split over three physical lines; we did that
to make the line fit on the page while still being readable. The line sets the content of a
label to the hello message and then adds the current time: the method call
DateTime.Now.Hour.ToString() returns a string (converted by ToString())
representing the current hour, and the plus (+) operator concatenates the strings
together to create a string with the hour and minute of the current time.
Between lines 9 and 16 we have a normal HTML document, except for line 14,
which uses the new ASP.NET capability to run a control on the server and render it on
the client using standard HTML code. Line 14 creates a label control on the server
(runat="server"), sets the ID so we can refer to it by name as we did in line 4, and
it is identified as an ASP.NET control (asp:).
Congratulations, you have created your first ASP.NET application (that’s a big word
for one page of code, but it does something, so we call it an application). Now you can
P:\010Comp\All-in-1\443-6\ch11.vp
Monday, August 26, 2002 11:44:12 AM
Color profile: Generic CMYK printer profile
Composite Default All-In-One
All-In-One
screen / MCAD/MCSD
/ MCSD Visual
Visual
C#C#
.NET
.NET
Certification
Certification
All-in-One
All-in-One
Exam
Exam
Guide
Guide
/ Rempel
/ Rempel
& Lind
& Lind
/ 222443-6
/ 222443-6
/ Chapter
/ Chapter
11
11
P:\010Comp\All-in-1\443-6\ch11.vp
Monday, August 26, 2002 11:44:13 AM
Color profile: Generic CMYK printer profile
All-In-One / MCAD/MCSD Visual C# .NET Certification All-in-One Exam Guide / Rempel & Lind / 222443-6 / Chapter
Composite Default screen
11
PART III
The Request Object
When a web client (a browser) requests a web page from a web server, the server collects
the client’s HTTP request in the ASP.NET Request object. The information that is en-
capsulated in the Request object includes details about the client system, including
the operating system, browser version, language preferences, form data from the client,
and so on. The information is a gold mine for the web server – it can glean information
from the Request object to be able to customize the output to meet the user’s system
configuration and language preference.
We do not have to gather all this information from the user by asking the user to
fill in forms manually. The entire HTTP request is packaged into a single object that in
essence holds the client’s ID and wishes, effectively saying “This is who I am, and this is
my request from you”. Some properties in the Request object are listed in Table 11-3.
Figure 11-4
The relationship
between the
.aspx page
and the ASP.NET
objects
P:\010Comp\All-in-1\443-6\ch11.vp
Monday, August 26, 2002 11:44:13 AM
Color profile: Generic CMYK printer profile
Composite Default All-In-One
screen / MCAD/MCSD Visual C# .NET Certification All-in-One Exam Guide / Rempel & Lind / 222443-6 / Chapter
11
The following example will use some of the properties to list the browser informa-
tion from the client’s browser.
P:\010Comp\All-in-1\443-6\ch11.vp
Monday, August 26, 2002 11:44:13 AM
Color profile: Generic CMYK printer profile
All-In-One / MCAD/MCSD Visual C# .NET Certification All-in-One Exam Guide / Rempel & Lind / 222443-6 / Chapter
Composite Default screen
11
PART III
The Text property of the lblMessage label is assigned the value of the bc.Type
property.
NOTE Buffering works like the vehicles on a freeway: one car can transport
5 passengers while we can transport 60 passengers in a bus. It would be
less efficient to transport just one passenger in a bus—the bus uses more
resources (gas, tires, and the like). The same is true for the TCP/IP protocols.
If we send one character at the time back to the client, we will spend more resources on
the overheads than if we can return larger amounts of data at one time.
P:\010Comp\All-in-1\443-6\ch11.vp
Monday, August 26, 2002 11:44:13 AM
Color profile: Generic CMYK printer profile
Composite Default All-In-One
screen / MCAD/MCSD Visual C# .NET Certification All-in-One Exam Guide / Rempel & Lind / 222443-6 / Chapter
11
The following example uses the Response object and the buffer to return data to the
client. The first example will write some characters to the browser using buffering; you
will see that the entire screen will be drawn in one operation:
P:\010Comp\All-in-1\443-6\ch11.vp
Monday, August 26, 2002 11:44:13 AM
Color profile: Generic CMYK printer profile
All-In-One / MCAD/MCSD Visual C# .NET Certification All-in-One Exam Guide / Rempel & Lind / 222443-6 / Chapter
Composite Default screen
11
This program uses a code block between the two tags <% and %>, and this code will write
2000 Euro symbols in the browser using the buffer. We use a do ... while loop to
generate the characters.
In the next example, we will turn off the buffering. Notice how the display is drawn
one character at the time.
PART III
<%@ Page Language="c#" %>
<html>
<body>
<%
int i = 0;
Response.Buffer = false;
do
{
Response.Write(" @ ");
i++;
} while (i < 2000);
%>
</body>
<html>
In this example, we turned off the buffering by setting the Response.Buffer prop-
erty to False—the display will be in steps. The effect might be more pronounced on
slower servers.
State or No State
The traditional Internet model is one where clients and servers operate in a stateless
mode, meaning that the server never remembers the client once it has disconnected
P:\010Comp\All-in-1\443-6\ch11.vp
Monday, August 26, 2002 11:44:14 AM
Color profile: Generic CMYK printer profile
Composite Default All-In-One
screen / MCAD/MCSD Visual C# .NET Certification All-in-One Exam Guide / Rempel & Lind / 222443-6 / Chapter
11
from the server. However, there are many very good reasons why you want to remember
the client, or to keep state:
• State An application that maintains state will remember (or persist) the data
defining the client, such as the name, e-mail address, customer number, phone
number, and so on. Keeping state has a price tag—we must store the data
somewhere and possibly send and receive extra data across the network.
• Stateless A stateless application never remembers anything about the client.
There is no overhead associated with storing data.
One common debate among web developers is where to store the shopping cart in an
e-mall: on the client or on the server? ASP.NET gives you a large number of solutions
that will help you maintain state in your web application, either by storing the informa-
tion on the server in the objects provided for this purpose (the Application and
Session objects) or on the client using cookies.
State is the common term for any data defining an object, so we can refer to state as
anything that is part of the application or the client session. When we refer to applica-
tion state, we mean data that is globally used in the entire application, while session
state refers to data that is focused on one user (one session). The following list should
give you some ideas of what we mean by the term state:
• The string that contains the company addresses and phone information of the
web sites company is application state.
P:\010Comp\All-in-1\443-6\ch11.vp
Monday, August 26, 2002 11:44:14 AM
Color profile: Generic CMYK printer profile
All-In-One / MCAD/MCSD Visual C# .NET Certification All-in-One Exam Guide / Rempel & Lind / 222443-6 / Chapter
Composite Default screen
11
The following two sections will explore application and session state through the
ASP.NET objects we use to keep that state.
PART III
Application["Address"] = "42 North Galaxy Road";
This line creates a variable in the Application object that is given the name Address
and assigned the value “42 North Galaxy Road”. Once the variable is created, we can
access it through code similar to this:
Response.Write(Application["Address"]);
The following example is made up from two pages that must be opened one after the
other (we will solve that inconvenience later in this section). The first page is called
SetApplication.aspx.
P:\010Comp\All-in-1\443-6\ch11.vp
Monday, August 26, 2002 11:44:14 AM
Color profile: Generic CMYK printer profile
Composite Default All-In-One
screen / MCAD/MCSD Visual C# .NET Certification All-in-One Exam Guide / Rempel & Lind / 222443-6 / Chapter
11
EXAM TIP Note that the two pages communicated with eachother through
the Application object.
The common procedure for initiating the Application object’s variables is to use
the global.asax file; it is the entry point to the web application and is the common
location for initialization code. The equivalent global.asax code is seen in the
following example:
Application.Lock();
Application["Variable"] = "10";
Application.UnLock();
The Lock() method is a blocking call. It will wait for access to the lock before return-
ing to the program, ensuring that changes are not permitted from more than one pro-
cess at a time. After modifying the data, you call the UnLock() method to let other
pages access the data.
Application state should be used to store global data that is frequently used. There is a
danger with using too many Application object variables because the object is stored
in memory and gets larger with each item stored in it. If the Application object is
heavily used, you should consider using the web.config file instead. The web.
config file is used to configure the web application without actually storing the data in
the Application object. For more information on the web.config file, see Chapter 17.
P:\010Comp\All-in-1\443-6\ch11.vp
Monday, August 26, 2002 11:44:14 AM
Color profile: Generic CMYK printer profile
All-In-One / MCAD/MCSD Visual C# .NET Certification All-in-One Exam Guide / Rempel & Lind / 222443-6 / Chapter
Composite Default screen
11
• A shopping cart with the items the user has selected while browsing through
your e-mall.
• Custom settings for the client.
• Client preferences (language, formatting, and the like).
PART III
• Personalization data (name, address, e-mail address, phone, and so on).
You assign values to Session variables the same way you did with the Applica-
tion variables. To set the Name variable to “Garfield O’Reilly”, you would use the
following statement:
The Session object has a number of methods and properties that are useful
in working with the clients session with the web server. Table 11-6 lists the most com-
monly used members.
Session variables are visible only to pages opened in the session, which means there
will never be more than one user modifying any values, so we do not need to lock the
session object for changes.
The Session object is used to store variables related to each session on the web
server. The default timeout setting for the Session object’s lifetime is 20 minutes; if
you have a large number of visitors, you will want to lower the timeout value so the
server will drop the unused Session objects earlier.
Description
Clear() Clears all values from session state.
Count Gets the number of items in the session-state collection.
SessionID Gets the unique session ID used to identify the session.
Timeout Gets and sets the time-out period (in minutes) allowed between requests
before the session-state provider terminates the session.
Table 11-6 The Commonly Used Members of the Session Object
P:\010Comp\All-in-1\443-6\ch11.vp
Monday, August 26, 2002 11:44:14 AM
Color profile: Generic CMYK printer profile
Composite Default All-In-One
screen / MCAD/MCSD Visual C# .NET Certification All-in-One Exam Guide / Rempel & Lind / 222443-6 / Chapter
11
Summary
In this chapter, you have learned about the Internet standards and the different architec-
tures used to build web sites for the Internet over the years. You explored client-side and
server-side execution while taking a look at ASP.NET.
The most important portion of this chapter for the exam is the ASP.NET object
model. You can expect a number of questions that refer to the objects in the model and
about how to use them. As a matter of fact, the objects are so important that we will re-
visit them a number of times in this part of the book.
Now you have enough information to start working with the ASP.NET pages. In the
next chapter you will explore the ASP.NET page with a focus on Web Forms and server
controls.
Test Questions
1. What definition correctly defines a label server control with the name set to
lblHoop?
A. <asp:Label name="lblHoop" runat="server" />
B. <Label id="lblHoop" runat="server" />
C. <asp:label id="lblHoop" runat="server" />
D. <server label name="lblHoop" runat="asp" />
2. What ASP.NET object encapsulates the user’s data as it is sent from a form
in a page?
A. The Session object.
B. The Application object.
C. The Response object.
D. The Request object.
E. The Server object.
3. What important standard is used to connect client browsers with web servers?
A. HTTP
B. TCP/IP
C. ASP.NET
D. HTML
4. What ASP.NET object is used to get information about the web servers
hostname?
A. The Session object.
B. The Application object.
P:\010Comp\All-in-1\443-6\ch11.vp
Monday, August 26, 2002 11:44:15 AM
Color profile: Generic CMYK printer profile
All-In-One / MCAD/MCSD Visual C# .NET Certification All-in-One Exam Guide / Rempel & Lind / 222443-6 / Chapter
Composite Default screen
11
PART III
B. Quick Basic
C. C
D. LiveScript
7. What is the name of the process the browser uses to find the address of
a web server?
A. DMZ
B. DNS
C. Active Directory
D. Database lookup
8. How many rules are there regarding a well formed XML document?
A. Nine
B. Three
C. Six
D. Two
9. What line in the following XML document will generate an error?
1 <?xml version="1.0">
2 <employees>
3 <employee>
4 <name>Bob Andcarrol</Name>
5 </employee>
6 <employee>
7 <Name>Robert Burns</Name>
8 </employee>
9 </employees>
A. 6
B. 4
P:\010Comp\All-in-1\443-6\ch11.vp
Monday, August 26, 2002 11:44:15 AM
Color profile: Generic CMYK printer profile
Composite Default All-In-One
screen / MCAD/MCSD Visual C# .NET Certification All-in-One Exam Guide / Rempel & Lind / 222443-6 / Chapter
11
P:\010Comp\All-in-1\443-6\ch11.vp
Monday, August 26, 2002 11:44:15 AM
Color profile: Generic CMYK printer profile
All-In-One / MCAD/MCSD Visual C# .NET Certification All-in-One Exam Guide / Rempel & Lind / 222443-6 / Chapter
Composite Default screen
11
Test Answers
1. C.
2. D.
3. B.
4. E.
5. A.
PART III
6. A.
7. B.
8. C.
9. B. The opening and closing elements do not match, <name> </Name>,
XML is case sensitive.
10. A.
11. D.
12. A.
13. D.
14. B.
15. B.
P:\010Comp\All-in-1\443-6\ch11.vp
Monday, August 26, 2002 11:44:15 AM