Introduction to ASP.NET Server Controls
When you create Web Forms pages, you can use these types of controls:
- HTML server controls HTML elements exposed to the server so you can program them. HTML server controls expose an object model that maps very closely to the HTML elements that they render.
- Web server controls Controls with more built-in features than HTML server controls. Web server controls include not only form-type controls such as buttons and text boxes, but also special-purpose controls such as a calendar. Web server controls are more abstract than HTML server controls in that their object model does not necessarily reflect HTML syntax.
- Validation controls Controls that incorporate logic to allow you to test a user's input. You attach a validation control to an input control to test what the user enters for that input control. Validation controls are provided to allow you to check for a required field, to test against a specific value or pattern of characters, to verify that a value lies within a range, and so on.
- User controls Controls that you create as Web Forms pages. You can embed Web Forms user controls in other Web Forms pages, which is an easy way to create menus, toolbars, and other reusable elements.
Note You can also create output for mobile devices. To do so, you use the same ASP.NET page framework, but you create Mobile Web Forms instead of Web Forms pages and use controls specifically designed for mobile devices. For details, see Creating ASP.NET Mobile Web Applications.
You can use all types of controls on the same page. The following sections provide more detail about ASP.NET server controls. For more information about validation controls, see Web Forms Validation. For information about user controls, see Introduction to Web User Controls.
Note In some situations, server controls require client script in order to function properly. If a user has disabled scripting in the browser, the controls might not function as you intend. For details, see ASP.NET Server Controls and Browser Capabilities.
HTML Server Controls
HTML server controls are HTML elements containing attributes that make them visible to — and programmable on — the server. By default, HTML elements on a Web Forms page are not available to the server; they are treated as opaque text that is passed through to the browser. However, by converting HTML elements to HTML server controls, you expose them as elements you can program on the server.
The object model for HTML server controls maps closely to that of the corresponding elements. For example, HTML attributes are exposed in HTML server controls as properties.
Any HTML element on a page can be converted to an HTML server control. Conversion is a simple process involving just a few attributes. As a minimum, an HTML element is converted to a control by the addition of the attribute RUNAT="SERVER". This alerts the ASP.NET page framework during parsing that it should create an instance of the control to use during server-side page processing. If you want to reference the control as a member within your code, you should also assign an ID attribute to the control.
The page framework provides predefined HTML server controls for the HTML elements most commonly used dynamically on a page: forms, the HTML <INPUT> elements (text box, check box, Submit button, and so on), list box (<SELECT>), table, image, and so on. These predefined HTML server controls share the basic properties of the generic control, and in addition, each control typically provides its own set of properties and its own event.
HTML server controls offer the following features:
- An object model that you can program against on the server using the familiar object-oriented techniques. Each server control exposes properties that allow you to manipulate the control's HTML attributes programmatically in server code.
- A set of events for which you can write event handlers in much the same way you would in a client-based form, except that the event is handled in server code.
- The ability to handle events in client script.
- Automatic maintenance of the control's state. If the form makes a round trip to the server, the values that the user entered into HTML server controls are automatically maintained when the page is sent back to the browser.
- Interaction with validation controls so you can easily verify that a user has entered appropriate information into a control.
- Data binding to one or more properties of the control.
- Support for HTML 4.0 styles if the Web Forms page is displayed in a browser that supports cascading style sheets.
- Pass-through of custom attributes. You can add any attributes you need to an HTML server control and the page framework will read them and render them without any change in functionality. This allows you to add browser-specific attributes to your controls.
For details about how to convert an HTML element to an HTML server control, see Adding HTML Server Controls to a Web Forms Page.
Web Server Controls
Web server controls are a second set of controls designed with a different emphasis. They do not map one-to-one to HTML server controls. Instead, they are defined as abstract controls in which the actual HTML rendered by the control can be quite different from the model that you program against. For example, a RadioButtonList Web server control might be rendered in a table or as inline text with other HTML.
Web server controls include traditional form controls such as buttons and text boxes as well as complex controls such as tables. They also include controls that provide commonly used form functionality such as displaying data in a grid, choosing dates, and so on.
Web server controls offer all of the features described above for HTML server controls (except one-to-one mapping to HTML elements) and these additional features:
- A rich object model that provides type-safe programming capabilities.
- Automatic browser detection. The controls can detect browser capabilities and create appropriate output for both basic and rich (HTML 4.0) browsers.
- For some controls, the ability to define your own look for the control using templates.
- For some controls, the ability to specify whether a control's event causes immediate posting to the server or is instead cached and raised when the form is submitted.
- Ability to pass events from a nested control (such as a button in a table) to the container control.
At design time in HTML view, the controls appear in your page in a format such as:
<asp:button attributes runat="server"/>
The attributes in this case are not those of HTML elements. Instead, they are properties of the Web control.
When the Web Forms page runs, the Web server control is rendered on the page using appropriate HTML, which often depends not only on the browser type but also on settings that you have made for the control. For example, a TextBox control might render as an <INPUT> tag or a <TEXTAREA> tag, depending on its properties.
Detailed how-to and reference documentation is available for each control separately. For more information, see Controls You Can Use on Web Forms Pages.