Udostępnij za pośrednictwem


ASP.NET Web Server Controls Overview

When you create ASP.NET Web 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 controls such as buttons and text boxes, but also special-purpose controls such as a calendar, menus, and a tree view control. 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 enable you to what users enter for input controls such as the TextBox control. Validation controls enable 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. For more information, see ASP.NET Validation Controls.

  • User controls   Controls that you create as ASP.NET Web pages. You can embed ASP.NET user controls in other ASP.NET Web pages, which is an easy way to create toolbars and other reusable elements. For more information, see ASP.NET User Controls.

    Note

    You can also create output for mobile devices. To do so, you use the same ASP.NET page framework, but you create Mobile ASP.NET Web pages instead of ASP.NET Web pages and use controls specifically designed for mobile devices. For details, see Creating ASP.NET Mobile Web Pages.

You can use all types of controls on the same page. The following sections provide more detail about ASP.NET server 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 Web Server Controls and Browser Capabilities.

HTML Server Controls

HTML server controls are HTML elements (or elements in other supported markup, such as XHTML) containing attributes that make them programmable in server code. By default, HTML elements on an ASP.NET Web page are not available to the server. Instead, they are treated as opaque text and 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 by adding the attribute runat="server". During parsing, the ASP.NET page framework creates instances of all elements containing the runat="server" attribute. 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: the form element, the input elements (text box, check box, Submit button), the select element, 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 familiar object-oriented techniques. Each server control exposes properties that enable you to manipulate the control's markup 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. When the page makes a round trip to the server, the values that the user entered into HTML server controls are automatically maintained and sent back to the browser.

  • Interaction with ASP.NET validation controls so you can verify that a user has entered appropriate information into a control.

  • Data binding to one or more properties of the control.

  • Support for styles if the ASP.NET Web 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 render them without any change in functionality. This enables you to add browser-specific attributes to your controls.

For details about how to convert an HTML element to an HTML server control, see How to: Add HTML Server Controls to a Web Page Using ASP.NET Syntax.

Web Server Controls

Web server controls are a second set of controls designed with a different emphasis. They do not necessarily map one-to-one to HTML server controls. Instead, they are defined as abstract controls in which the actual markup 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 markup.

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, displaying menus, and so on.

Web server controls offer all of the features described above for HTML server controls (except one-to-one mapping to 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 render appropriate markup.

  • For some controls, the ability to define your own layout 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 page is submitted.

  • Support for themes, which enable you to define a consistent look for controls throughout your site. For details, see ASP.NET Themes and Skins.

  • Ability to pass events from a nested control (such as a button in a table) to the container control.

The controls use syntax such as the following:

<asp:button attributes runat="server" id="Button1" />

The attributes in this case are not those of HTML elements. Instead, they are properties of the Web control.

When the ASP.NET Web page runs, the Web server control is rendered on the page using appropriate markup, 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 Individual ASP.NET Web Server Controls.

See Also

Concepts

ASP.NET Web Server Controls Overview