Web Forms: The Web Forms Programming Model
By Jeff Prosise
February 27, 2002
From Programming Microsoft .NET. Copyright 2002, Jeff Prosise. Reproduced by permission of Microsoft Press. All rights reserved.
Authors note: The following is extracted from the book Programming Microsoft .NET by Jeff Prosise (Microsoft Press, 2002, ISBN: 0-7356-1376-1) The book teaches readers how to take advantage of the .NET Framework. It covers the key programming models embodied in the .NET Framework, including Windows forms, Web forms, and XML Web services.
The Web Forms Programming Model
Editor's Note: This article continues Chapter 5, "Web Forms." The first part of this chapter can be obtain from HERE.
Calc.aspx demonstrates three important principles of the Web Forms programming model:
- A Web form's user interface is "declared" using a combination of HTML and server controls. Controls can be customized by using control properties as attributes in the tags that declare the controls. Controls are also bona fide objects that are instantiated and executed each time the page that hosts them is requested.
- Server controls fire events that can be handled by server-side scripts. In effect, ASP.NET abstracts the divide between client and server by creating the illusion that events are fired and handled on the same machine. In reality, events fire on the server when an external stimulus (such as the click of a button) causes the form to post back to the server.
- Server-side scripts aren't scripts in the conventional sense of the word. Unlike ASP scripts, which are interpreted rather than compiled and therefore run rather slowly, server-side scripts in ASP.NET are compiled to common intermediate language (CIL) and executed by the common language runtime. Although ASP.NET pages incur more processing overhead than static HTML pages, they tend to execute much faster than ASP pages.
You probably noticed the RunAt="server" attributes sprinkled throughout Calc.aspx. RunAt="server" is the key that unlocks the door to the magic of Web forms; it signals ASP.NET to "execute" the tag rather than treat it as static HTML. RunAt="server" is not optional. It must be used in every tag that ASP.NET is to process, including the <form> tag that marks the beginning of a form containing server controls.
Web Controls
TextBox, Button, and Label are server controls. They're also examples of Web controls-server controls defined in the FCL's System.Web.UI.WebControls namespace. The Web controls family includes almost 30 different control types that you can use in ASP.NET Web forms. The following table lists the Web controls provided in version 1.0 of the .NET Framework class library:
Web Controls
|
Class Name |
Description |
|
AdRotator |
Displays rotating banners in Web
forms |
|
Button |
Generates submit buttons |
|
Calendar |
Displays calendars with selectable
dates |
|
CheckBox |
Displays a check box in a Web
form |
|
CheckBoxList |
Displays a group of check
boxes |
|
CompareValidator |
Validates user input by comparing
it to another value |
|
CustomValidator |
Validates user input using the
algorithm of your choice |
|
DataGrid |
Displays data in tabular
format |
|
DataList |
Displays items using single column
or multicolumn lists |
|
DropDownList |
Generates HTML drop-down
lists |
|
HyperLink |
Generates hyperlinks |
|
Image |
Displays images in Web
forms |
|
ImageButton |
Displays graphical push
buttons |
|
Label |
Generates programmable text
fields |
|
LinkButton |
Generates hyperlinks that post
back to the server |
|
ListBox |
Generates HTML list
boxes |
|
Literal |
Generates literal text in a Web
form |
|
Panel |
Groups other controls |
|
RadioButton |
Displays a radio button in a Web
form |
|
RadioButtonList |
Displays a group of check
boxes |
|
RangeValidator |
Verifies that user input falls
within a specified range |
|
RegularExpressionValidator |
Validates user input using regular
expressions |
|
Repeater |
Displays items in simple
lists |
|
RequiredFieldValidator |
Verifies that an input field isnt
empty |
|
Table |
Generates HTML tables |
|
TextBox |
Generates text input
fields |
|
ValidationSummary |
Displays a summary of validation
errors |
|
Xml |
Displays XML documents and
documents generated from XML using XSLT |
Some Web controls are simple devices that produce equally simple HTML. Others produce more complex HTML, and some even return client-side script. Calendar controls, for example, emit a rich mixture of HTML and JavaScript. It's not easy to add a calendar to a Web page by hand (especially if you want dates in the calendar to be clickable), but calendars are no big deal in Web forms: you simply include an <asp:Calendar> tag in an ASPX file. DataGrid is another example of a sophisticated control type. One DataGrid control can replace reams of old ASP code that queries a database and returns the results in a richly formatted HTML table. You'll learn all about the DataGrid and other Web controls in the next chapter.