Home > Articles > Programming > ASP .NET

This chapter is from the book

Creating User Interfaces with Web Controls

The .NET framework provides a number of user-interface components you can use.

These components are divided into several categories:

  • HTML controls—An object-oriented way of creating standard HTML page elements such as text boxes and buttons

  • Web forms controls—Objects that can incorporate standard and dynamic HTML into rich user-interface elements

  • Validation controls—A type of Web form control specifically geared toward validating user input on a form, often without causing a round trip to the server. Validation controls are covered in Chapter 12, "Creating Database Applications with ADO.NET."

This section will show you how to use the controls that are included with the .NET framework. You can also create your own Web forms controls; this is covered in Chapter 10.

Programming Web Forms Controls

The first step to using Web forms controls on a page is to create an ASP.NET Web form. You do this by creating a conventional HTML FORM tag with the ASP.NET runat="server" attribute, as shown in Listing 3.26.

Listing 3.26 Basic ASP.NET Web Form Declaration

<form id='myform' method='post' runat='server'>


Control Event Model

Web controls raise events of their own as well as the events of the classes they inherit.

Web controls that inherit from System.Web.UI.WebControl.WebControl raise the events listed in Table 3.3.

Table 3.3 Events Raised by Web Controls




Occurs when the control binds to a data source. (For more information on data binding, see Chapter 12.)


Occurs when the control is destroyed; this is always the last event raised by the control.


Occurs when the control is first created; this is always the first event raised by the control.


Occurs when the control is loaded on the page.


Occurs when the control is about to be rendered (displayed on the screen).


Occurs when the control is unloaded from memory.

Taking Advantage of Postback and View State

Postback is the ASP.NET feature that enables you to detect whether a form has been submitted to the server. View State is a related concept that causes the contents of a posted-back form to be preserved.

The concepts of postback and View State grew out of a frustration with traditional Web server scripting paradigms. In a Web form, as with most HTTP Web requests, the page is completely destroyed and re-created each time it is submitted to the server. But in many, if not most cases, you want certain values (such as the contents of a Search box or the selection you've made in a list of custom preferences) to remain on the page. Without View State, you'd have to write code to make this happen, and without postback detection, it's tricky to determine whether the existing form data needs to be displayed.

You can determine whether an ASP.NET Web form is being posted back to the server by inspecting the IsPostBack property of the Page object. The first time a user accesses the page, IsPostBack will be false. At this time, you typically perform initialization work in code. This can be as straightforward as setting defaults in controls or as involved as performing a relational database query to serve as the basis for display on the page.


It is possible to manipulate postback directly in code. To do this, you implement the IPostBackEventHandler interface in your page.

Earlier in this chapter, Listing 3.5 demonstrated the HTML source of an ASP.NET Web page that contains chunks of encoded View State. Listing 3.27 shows a snippet of that voluminous code listing.

Listing 3.27 HTML Source for an ASP.NET Page Highlighting View State Information

  <form name="WebForm1" method="post" action="calendar.aspx" id="WebForm1">
<input type="hidden" name="__VIEWSTATE" value="dDw1MzYzNjkxODU7Oz4=" />

    <table id="Calendar1" cellspacing="0" cellpadding="2" border="0" 
      <td colspan="7" style="background-color:Silver;">
        <table cellspacing="0" border="0" 
          <td style="width:15%;">
           <a href="javascript:__doPostBack('Calendar1','prevMonth')"

Note that we've mercifully deleted the majority of the source code for brevity. You won't have to write this code yourself; fortunately, ASP.NET generates it for you from the high-level ASP.NET controls you specify.

You can see that the View State information is embedded in the page as a hidden control with the name __VIEWSTATE. The value is encoded, but not encrypted.

You should also be able to see that a hyperlink (a tag) toward the end of the code snippet is attached to an automatically generated JavaScript procedure called __doPostBack. This procedure parses the contents of View State and submits the form back to the server.

The ultimately cool thing about View State is that you don't have to lift a finger to make it work. You may choose to turn it off, however; getting rid of View State when you don't need it can provide a bit of a performance gain. To turn View State off for a particular control, set the control's ViewState property to false. (View State can also be shut off for a particular page or an entire Web application by using @Page directives, described earlier in this chapter, and Web.config settings, described in Chapter 6.)

Mobile Controls

The Microsoft.NET framework supports devices with small form factors and limited capabilities, such as mobile phones and Pocket PCs. These devices have demands on the user-interface developer that are totally different from the challenges presented by user-interface design on a desktop PC.

An in-depth discussion of mobile Web forms is beyond the scope of this book; however, many of the techniques discussed in this chapter will give you a good foundation for creating applications for mobile devices. Because the technical implementation (involving HTML, Dynamic HTML, WAP, and so forth) is abstracted behind the .NET Base Class Library, you can use similar programming techniques to code for Windows and Windows CE.

Data Binding

It is possible to bind a data source to controls in ASP.NET. This works both for rich Web forms controls and HTML controls.

For more information on data binding, see Chapter 12.

Determining Browser Capabilities

When you're creating a corporate intranet, you can often specify corporate standards for a Web browser. Although this tends to limit user choice, this is useful for the developer because it means you can utilize the capabilities of a specific Web browser. For Web sites that are intended to be used by the general public, though, you can't know ahead of time what kind of browser the users will have. In this case, you may find it useful to have a programmatic way of determining things about the user's browser. For example, does the user's browser support JavaScript (or, perhaps, did the user deactivate JavaScript support)? How about cookies? Even though most modern browsers support them, users can choose to turn those off, too.

The HttpBrowserCapabilities class, found in the System.Web namespace, provides these capabilities in ASP.NET. An instance of an HttpBrowserCapabilities object is contained by the ASP.NET Request object (in this context an instance of the object, called Browser, is created for you automatically when a page is requested from the server). Listing 3.28 shows how to use this pre-created object to display information about the browser.

Listing 3.28 Complete Listing of All HttpBrowserCapabilities Contained in the Request Object

<%@ PAGE language='VB' debug='true' trace='false' %>

<TITLE>ASP.NET Browser Capabilities</TITLE>

<table width="350" border="0" cellspacing="1" cellpadding="3" 
 <tr bgcolor="#FFFFFF"> 
  <td>Supports ActiveX Controls</td>
  <td><% =Request.Browser.ActiveXControls %></td>
 <tr bgcolor="#FFFFFF"> 
  <td>Is an America Online Browser</td>
  <td><% =Request.Browser.AOL %></td>
 <tr bgcolor="#FFFFFF"> 
  <td>Supports background sounds</td>
  <td><% =Request.Browser.BackgroundSounds %></td>
 <tr bgcolor="#FFFFFF"> 
  <td>Is a beta version browser</td>
  <td><% =Request.Browser.Beta %></td>
 <tr bgcolor="#FFFFFF"> 
  <td>Browser name (user-agent)</td>
  <td><% =Request.Browser.Browser %></td>
 <tr bgcolor="#FFFFFF"> 
  <td>Supports Channel Definition Format</td>
  <td><% =Request.Browser.CDF %></td>
 <tr bgcolor="#FFFFFF"> 
  <td>Common Language Runtime version</td>
  <td><% =Request.Browser.ClrVersion %></td>
 <tr bgcolor="#FFFFFF"> 
  <td>Cookies available</td>
  <td><% =Request.Browser.Cookies %></td>
 <tr bgcolor="#FFFFFF"> 
  <td>Is this a Web search engine (&quot;crawler&quot;)?</td>
  <td><% =Request.Browser.Crawler %></td>
 <tr bgcolor="#FFFFFF"> 
  <td>Version of JavaScript (ECMAScript) supported</td>
  <td><% =Request.Browser.EcmaScriptVersion %></td>
 <tr bgcolor="#FFFFFF"> 
  <td>Supports frames</td>
  <td><% =Request.Browser.Frames %></td>
 <tr bgcolor="#FFFFFF"> 
  <td>Supports client-side Java</td>
  <td><% =Request.Browser.JavaApplets %></td>
 <tr bgcolor="#FFFFFF"> 
  <td>Supports JavaScript (ECMAScript)</td>
  <td><% =Request.Browser.JavaScript %></td>
 <tr bgcolor="#FFFFFF"> 
  <td>Browser version</td>
   <% =Request.Browser.MajorVersion & "." & _
Request.Browser.MinorVersion %>
 <tr bgcolor="#FFFFFF"> 
  <td>Microsoft XML Document Object Model version</td>
  <td><% =Request.Browser.MsDomVersion %></td>
 <tr bgcolor="#FFFFFF"> 
  <td>Operating system platform</td>
  <td><% =Request.Browser.Platform %></td>
 <tr bgcolor="#FFFFFF"> 
  <td>Supports HTML tables</td>
  <td><% =Request.Browser.Tables %></td>
 <tr bgcolor="#FFFFFF"> 
  <td>Client browser type</td>
  <td><% =Request.Browser.Type %></td>
 <tr bgcolor="#FFFFFF"> 
  <td>Browser supports VBScript</td>
  <td><% =Request.Browser.VBScript %></td>
 <tr bgcolor="#FFFFFF"> 
  <td>Version of client browser</td>
  <td><% =Request.Browser.Version %></td>
 <tr bgcolor="#FFFFFF"> 
  <td>W3C HTML Document Object Model version</td>
  <td><% =Request.Browser.W3CDomVersion %></td>
 <tr bgcolor="#FFFFFF"> 
  <td>Running 16-bit Windows?</td>
  <td><% =Request.Browser.Win16 %></td>
 <tr bgcolor="#FFFFFF">
  <td>Running 32-bit Windows?</td>
  <td><% =Request.Browser.Win32 %></td>

I used godless, archaic render blocks to create this page, mainly to make it easier for me to create, but also to make it simpler to read.

If you have more than one kind of browser installed on your computer, you may find it interesting to navigate to this page using both of them to see the different capabilities reported by the Browser object. For example, I found it interesting to learn that JavaScript 1.2 comes with Internet Explorer 6.0 beta, but that Opera 5.0 comes with the (presumably bigger and badder) JavaScript 1.3. Also, Opera doesn't return operating system platform information to the server the way Internet Explorer does. It's just as well; with well-designed Web applications, the server shouldn't need to know what operating system the browser is running on anyway.

InformIT Promotional Mailings & Special Offers

I would like to receive exclusive offers and hear about products from InformIT and its family of brands. I can unsubscribe at any time.


Pearson Education, Inc., 221 River Street, Hoboken, New Jersey 07030, (Pearson) presents this site to provide information about products and services that can be purchased through this site.

This privacy notice provides an overview of our commitment to privacy and describes how we collect, protect, use and share personal information collected through this site. Please note that other Pearson websites and online products and services have their own separate privacy policies.

Collection and Use of Information

To conduct business and deliver products and services, Pearson collects and uses personal information in several ways in connection with this site, including:

Questions and Inquiries

For inquiries and questions, we collect the inquiry or question, together with name, contact details (email address, phone number and mailing address) and any other additional information voluntarily submitted to us through a Contact Us form or an email. We use this information to address the inquiry and respond to the question.

Online Store

For orders and purchases placed through our online store on this site, we collect order details, name, institution name and address (if applicable), email address, phone number, shipping and billing addresses, credit/debit card information, shipping options and any instructions. We use this information to complete transactions, fulfill orders, communicate with individuals placing orders or visiting the online store, and for related purposes.


Pearson may offer opportunities to provide feedback or participate in surveys, including surveys evaluating Pearson products, services or sites. Participation is voluntary. Pearson collects information requested in the survey questions and uses the information to evaluate, support, maintain and improve products, services or sites, develop new products and services, conduct educational research and for other purposes specified in the survey.

Contests and Drawings

Occasionally, we may sponsor a contest or drawing. Participation is optional. Pearson collects name, contact information and other information specified on the entry form for the contest or drawing to conduct the contest or drawing. Pearson may collect additional personal information from the winners of a contest or drawing in order to award the prize and for tax reporting purposes, as required by law.


If you have elected to receive email newsletters or promotional mailings and special offers but want to unsubscribe, simply email information@informit.com.

Service Announcements

On rare occasions it is necessary to send out a strictly service related announcement. For instance, if our service is temporarily suspended for maintenance we might send users an email. Generally, users may not opt-out of these communications, though they can deactivate their account information. However, these communications are not promotional in nature.

Customer Service

We communicate with users on a regular basis to provide requested services and in regard to issues relating to their account we reply via email or phone in accordance with the users' wishes when a user submits their information through our Contact Us form.

Other Collection and Use of Information

Application and System Logs

Pearson automatically collects log data to help ensure the delivery, availability and security of this site. Log data may include technical information about how a user or visitor connected to this site, such as browser type, type of computer/device, operating system, internet service provider and IP address. We use this information for support purposes and to monitor the health of the site, identify problems, improve service, detect unauthorized access and fraudulent activity, prevent and respond to security incidents and appropriately scale computing resources.

Web Analytics

Pearson may use third party web trend analytical services, including Google Analytics, to collect visitor information, such as IP addresses, browser types, referring pages, pages visited and time spent on a particular site. While these analytical services collect and report information on an anonymous basis, they may use cookies to gather web trend information. The information gathered may enable Pearson (but not the third party web trend services) to link information with application and system log data. Pearson uses this information for system administration and to identify problems, improve service, detect unauthorized access and fraudulent activity, prevent and respond to security incidents, appropriately scale computing resources and otherwise support and deliver this site and its services.

Cookies and Related Technologies

This site uses cookies and similar technologies to personalize content, measure traffic patterns, control security, track use and access of information on this site, and provide interest-based messages and advertising. Users can manage and block the use of cookies through their browser. Disabling or blocking certain cookies may limit the functionality of this site.

Do Not Track

This site currently does not respond to Do Not Track signals.


Pearson uses appropriate physical, administrative and technical security measures to protect personal information from unauthorized access, use and disclosure.


This site is not directed to children under the age of 13.


Pearson may send or direct marketing communications to users, provided that

  • Pearson will not use personal information collected or processed as a K-12 school service provider for the purpose of directed or targeted advertising.
  • Such marketing is consistent with applicable law and Pearson's legal obligations.
  • Pearson will not knowingly direct or send marketing communications to an individual who has expressed a preference not to receive marketing.
  • Where required by applicable law, express or implied consent to marketing exists and has not been withdrawn.

Pearson may provide personal information to a third party service provider on a restricted basis to provide marketing solely on behalf of Pearson or an affiliate or customer for whom Pearson is a service provider. Marketing preferences may be changed at any time.

Correcting/Updating Personal Information

If a user's personally identifiable information changes (such as your postal address or email address), we provide a way to correct or update that user's personal data provided to us. This can be done on the Account page. If a user no longer desires our service and desires to delete his or her account, please contact us at customer-service@informit.com and we will process the deletion of a user's account.


Users can always make an informed choice as to whether they should proceed with certain services offered by InformIT. If you choose to remove yourself from our mailing list(s) simply visit the following page and uncheck any communication you no longer want to receive: www.informit.com/u.aspx.

Sale of Personal Information

Pearson does not rent or sell personal information in exchange for any payment of money.

While Pearson does not sell personal information, as defined in Nevada law, Nevada residents may email a request for no sale of their personal information to NevadaDesignatedRequest@pearson.com.

Supplemental Privacy Statement for California Residents

California residents should read our Supplemental privacy statement for California residents in conjunction with this Privacy Notice. The Supplemental privacy statement for California residents explains Pearson's commitment to comply with California law and applies to personal information of California residents collected in connection with this site and the Services.

Sharing and Disclosure

Pearson may disclose personal information, as follows:

  • As required by law.
  • With the consent of the individual (or their parent, if the individual is a minor)
  • In response to a subpoena, court order or legal process, to the extent permitted or required by law
  • To protect the security and safety of individuals, data, assets and systems, consistent with applicable law
  • In connection the sale, joint venture or other transfer of some or all of its company or assets, subject to the provisions of this Privacy Notice
  • To investigate or address actual or suspected fraud or other illegal activities
  • To exercise its legal rights, including enforcement of the Terms of Use for this site or another contract
  • To affiliated Pearson companies and other companies and organizations who perform work for Pearson and are obligated to protect the privacy of personal information consistent with this Privacy Notice
  • To a school, organization, company or government agency, where Pearson collects or processes the personal information in a school setting or on behalf of such organization, company or government agency.


This web site contains links to other sites. Please be aware that we are not responsible for the privacy practices of such other sites. We encourage our users to be aware when they leave our site and to read the privacy statements of each and every web site that collects Personal Information. This privacy statement applies solely to information collected by this web site.

Requests and Contact

Please contact us about this Privacy Notice or if you have any requests or questions relating to the privacy of your personal information.

Changes to this Privacy Notice

We may revise this Privacy Notice through an updated posting. We will identify the effective date of the revision in the posting. Often, updates are made to provide greater clarity or to comply with changes in regulatory requirements. If the updates involve material changes to the collection, protection, use or disclosure of Personal Information, Pearson will provide notice of the change through a conspicuous notice on this site or other appropriate way. Continued use of the site after the effective date of a posted revision evidences acceptance. Please contact us if you have questions or concerns about the Privacy Notice or any objection to any revisions.

Last Update: November 17, 2020