HTML Server Controls
One of the most common tasks any web developer will perform is the collecting and storing of information
from the user. This could simply be a name and e-mail address, or it might
range across a whole gamut of details including address, phone number, fax,
credit card, and delivery address. Whatever the information you want to gather,
the processing cannot be performed within the confines of HTML on the browser
alone. Therefore, you need to send the information to the web server for
processing. Once the web server has extracted the requisite information, an
updated version of the page, or possibly on some occasions, a separate second
page, is returned to the user.
Information is transmitted via web pages
via a form, and in HTML,
there are specialized tags for dealing with these. HTML forms contain a set of
HTML controls, such as textboxes, checkboxes, and drop-down lists, all of which
aid the passage of information from the user to the server. On top of this,
ASP.NET adds its own extra controls for dealing with forms. With these, ASP.NET
introduces some new concepts to the control of forms. Previously with HTML
forms, for example, when you selected a particular form control, the web
browser dealt with the entire handling of the form data until it was passed to
the web server. However, there are now new features of the .NET Framework, such
as remembering what text you've typed into a textbox, or what selection you
made in a listbox between page refreshes, which is carried out on the server.
While we're going to be using some ASP.NET
code to handle the interchange of form data, we won't be explaining all of the
facets of ASP.NET that we will use, only those that relate directly to forms.
To keep things clear, we're not going to start explaining exactly how it all
works until later chapters, but by the end of the chapter, you will understand
how to send information between browser and server via ASP.NET code. You'll
also be introduced to some of the terminology associated with this process.
In this chapter we will cover:
q
The client-server model of the web
q
HTML forms and web forms
q
HTML form controls
q
Server Controls
Forms in the Real World
The main focus of the chapter is using
forms, and implicitly, the transfer of
data from the browser to the server. Before we start delving into the inner
workings of the form, we'll describe a few situations in which forms would be
required in the business world, to see what kind of things they are used for.
If you take a look at a few commercial web sites, you'll find that forms are
usually provided in the same kinds of situations, such as:
q
To take information from a user. This
could be for the purpose of registration, the purchase of a product, or joining
an e-mail list/form/newsgroup.
q
To take note of a user's preferences
so that we can customize other pages in the site to include relevant
information, and exclude things that don't interest them.
q
To provide a questionnaire or survey
on how a business may go about improving the service that it offers.
q
To act as
front end for a forum or newsgroup, where a user can enter and edit their text
online.
These are just a few examples of some
common everyday situations. In this chapter, we're just going to approach one
facet of such a situation for a fictional business. This business, Feiertag Holidays,
requires a web site that will allow users to view different destinations within
Europe, and then browse through details of different hotels at each
destination. Our forms will allow the user to select a destination and send the
details of their choice to the web server. We will see how ASP.NET forms can be
used to expedite the process.
One of the main advantages of using this in
a typical full-blown business application is that rather than having to create
a unique page for each separate destination, as might have to be done in the
real world, we can create a generic page, dynamically generated by ASP.NET,
which fills in details about each destination, and therefore requires a lot
less coding.
However, before we get into that, it's best
to take a quick overview of forms, and see the ways in which ASP.NET affects
them.
Web Pages, HTML Forms, and Web Forms
With the introduction of any new technology comes new
terminology and jargon. ASP.NET is no different in this respect. With ASP.NET,
even the terms you use to describe a simple web page have been updated to more
accurately describe the processes that are going on within them. To avoid
confusion, we're going to start by defining a few familiar concepts and their
ASP.NET equivalents. Let's begin with a web
page.
Everybody reading this should know what a
web page is – it's just a bundle of HTML code, made up of markup tags,
nominally beginning and ending with <html> and </html> tags.
The web page is placed on a machine, known as a web server, and it is the job
of the web server to make that page available to all and sundry. Whether that
page contains text, graphics, movies, sound, or bits and pieces of other
languages/technologies or whether it was dynamically generated is of no concern
to us.
An HTML
form is a web page that contains one or more form
controls (grouped together inside an HTML <form> element) that allow the user to enter information on the web page
and send that information back to the web server. Commonly used form controls
include buttons, textboxes, checkboxes, and drop-down lists. The user fills in
details and presses a 'submit' button to send their data back to the web
server.
Although you don't need anything more than
HTML to send form data to the server, the server needs some sort of extra
technology (in this case, ASP.NET) to actually do anything with the information it receives. HTML pages containing
forms are typically saved with the suffix .html (or sometimes .htm).
ASP.NET goes one further than this and introduces
a new concept, the web form.
The web form is similar to the HTML forms we've seen before, and visually you
wouldn't be able to differentiate between an HTML form and a web form, but it's
what ASP.NET does behind the scenes with the web forms operation that make it
quite a different entity. Firstly, the term web form refers to the grouping of
two distinct blocks of code:
q
The HTML template
containing page layout information and ASP.NET server controls (see
below). This is responsible for the presentation of the web form on the
browser.
q
The ASP.NET code that
provides the web form's processing
logic. This is responsible for generating dynamic content to be displayed
within the web form. This content is typically exposed via server controls
defined in the HTML presentation block.
Although a web form may also be
an HTML form (that is, there's nothing to stop us using <form> elements inside an ASPX), remember that these two entities are
defined in quite distinct terms.
When we start using ASP.NET within our
web pages, and we create a web form, we can then use a new breed of ASP.NET server
controls within our HTML (we looked at these
very briefly in the last chapter). Not only do they duplicate the functionality
of many HTML elements (including the form controls), but they also do a lot
more besides. A server control has the appearance of an HTML-like element, but actually it only marks a point in the page
at which the server needs to generate a corresponding true-HTML element. The
advantage this offers over an HTML control is that we can create content for
the form, before returning the form to the browser, and we can generate this content
from just about anywhere in our code.
As
we've already seen, the ASP.NET code can be specified in a <script> block that may occur at any point within the ASPX file. We're
keeping it at the top of the code page, to help clarify the separation of presentation
and content. As we'll see in Chapter 15 though, we can ultimately place the
ASP.NET code into a completely separate file (a technique known as code
behind). What's important is that you recognize that when we talk about a web
form, we're referring to both these sections, regardless of where they are, or how they're
organized.
So, we know that it is possible for web
forms to use normal HTML form controls, but ASP.NET also comes with its own set
of web form controls that are run on the server. We will be using these in
preference most of the time, because they offer other advantages such as being
able to remember the state of the different controls, such as what text has
been typed into a textbox. These ASP.NET controls are run within specially modified
HTML <form runat="server"> tags, and are ASP.NET forms.
There are four different terms here that we
need to be clear about before we go any further:
q
A web page is any page
that contains just HTML (they can also contain script/other languages not covered by this book, but in this
book a web page will refer to pages containing only HTML)
q
An HTML form is an
HTML element that contains HTML form controls
q
A web form is any page
that combines ASP.NET code with an HTML template
q
An ASP.NET form is a
form inside a web form, that contains ASP.NET server controls
Because our discussion of forms is
essentially a discussion of how to transmit data from a browser back to the web
server, we need to start out by considering the whole process of data
transmission on the Web, so that we can put the role of forms into context.
It's worth emphasizing first though that no matter which technology you use, the
browser will always ultimately receive HTML.
So let's now look at how web browsers and
web servers work together to make web pages available to the world.
Buy Beginning ASP.NET with C# here
© Copyright 2002 Wrox Press
This chapter is written by David Sussman, et al
and taken from "Beginning ASP.NET with C#" published by Wrox Press Limited in June 2002; ISBN 1861007345; copyright © Wrox Press Limited 2002; all rights reserved.
No part of these chapters may be reproduced, stored in a retrieval system or transmitted in any form or by any means -- electronic, electrostatic, mechanical, photocopying, recording or otherwise -- without the prior written permission of the publisher, except in the case of brief quotations embodied in critical articles or reviews.
|
|