Main Page

  Previous Section Next Section

5.2 Web Controls

One of the challenges in developing web applications is that of providing support for different browsers that have different capabilities, proprietary extensions to HTML, and support for different scripting languages, while maintaining the desired level of functionality and consistency. The only way to render content consistently is to detect the browser type and send the appropriate version of the page to that browser. ASP.NET web controls relieve us of this burden by sniffing the browser type and sending the appropriate content based on the capabilities of the browser. ASP.NET server controls use HTML 3.2 for the down-level clients (older browsers that do not support DHTML and CSS) and can generate Dynamic HTML for the up-level clients (such as Internet Explorer 5.5 or later). In the current release of ASP.NET, the only controls that make extensive use of DHTML in up-level browsers are the validation controls (which are discussed in more detail in Section 5.4.8 later in this chapter). Other controls, such as the Button server control, use client-side JavaScript for initiating postbacks. These postback scripts are designed to work with any Javascript-compatible browser.

Web controls provide an abstract, consistent, and strongly typed object model. They are abstract because their object model does not necessarily reflect HTML syntax. These controls include standard controls like text boxes and radio buttons, as well as rich controls like calendars and data grids. Web controls are always declared with the ASP namespace prefix, sometimes using self-closing tags as follows:

<asp:textbox id="txtName" text="Hello, World!" runat="server" />

You can alternatively declare a web control by using an opening and closing tag pair. For certain controls, such as the Label and TextBox controls, any text contained between the opening and closing tags will be assigned to the Text property of the control. Thus, the following code fragment is equivalent to the previous one:

<asp:textbox id="txtName" runat="server">
   Hello, World!

Like element and attribute names in page declarations, the tag and attribute names used to create server controls declaratively are not case-sensitive. However, because the HTML 4.0 standard specifies that tags and attributes should be in lowercase, it's good coding practice to follow this guideline, even though server control tags are not sent to the browser.

When creating controls programmatically (as discussed later in this chapter), if the language you're using is case-sensitive (such as C#), you'll need to use the correct case when creating controls (e.g., "TextBox" versus "textbox"). Likewise, when you assign an ID to a control using the id attribute of a server control tag, case matters with a case-sensitive language. That is, given the following tag in a .aspx page written in C#:

<asp:Label id="myLabel" runat="server"/>

this code will cause a compiler error:

MyLabel.Text = "Hello, World!";

while this code will work correctly:

myLabel.Text = "Hello, World!";

The attributes of web controls declared using the ASP.NET syntax become the properties of the control, and you can access them programmatically. Unlike the HTML controls, the object model of the web controls does not necessarily reflect HTML syntax. The main reason for this behavior is that, depending on the attributes applied to a web control, it may render one of many HTML elements to the browser. For example, <asp:textbox> can render <input type="text">, <input type="password">, or <textarea>, based on the value of the TextBoxMode attribute supplied by the developer at design time.

      Previous Section Next Section