Server Controls


Server controls are components used in ASP.NET pages to define the user interface of a Web application. Server controls are the essential element of the Web Forms programming model. They form the basis of a modern, component-based, intuitive forms package that simplifies the development of Web user interfaces, similar to the way that the Microsoft Visual Basic forms package simplified Windows programming. At a high level, server controls provide abstractions of an underlying Web application and presentation technology. For example, the Web controls that ship as part of ASP.NET are abstractions of the HTML and HTTP technologies used in the creation of browser-based applications. These controls are described as abstractions because they do not directly represent HTML elements; instead, they offer a more abstract object model for creating those elements.

The Web Forms programming model greatly simplifies application development for page developers and designers and is characterized by the server control features and capabilities that follow.

  • Server controls create an intuitive and simpler programming model for the page developer by hiding the inconsistencies and complexities of the underlying technology. For example, the Button Web control maps directly to the <input type="submit"> HTML tag and hides the inconsistencies of the type attribute of the <input> tag. The TextBox Web control is slightly more sophisticated and maps to one of the multiple text entry tags provided by HTML: <input type="text"> , <input type="password"> , and <textarea> . At the other end of the spectrum, controls such as Calendar and DataGrid provide new functionality that is not inherently available in HTML, such as date selection and data binding.

  • They hide the differences between various browsers and viewing devices, as well as the various browser versions that a Web application might need to target. The ability of server controls to render content as appropriate to the target browser enables the page developer to write a single application that can target multiple viewing platforms at run time. Server controls contain the logic to provide the best possible experience by examining the capabilities of the requesting browser or device.

  • They function as true components, providing the same benefits that you might expect from working with components in other types of applications. Server controls provide a rich server-side programming model. They expose their functionality through properties and methods . They also provide an event-based programming model, which allows page developers to implement application logic in response to user interaction. The object model exposed by server controls is strongly typed ”unlike that of DHTML, which is loosely typed. Not only does being strongly typed lead to better server control performance, it also reduces programming errors.

  • They manage their state across postbacks and round-trips. A scalable Web application is stateless ”in other words, the application doesn't maintain data and state on the server corresponding to each user accessing the application. Server controls use a feature of the ASP.NET page framework known as view state to manage their state across individual Web requests . This feature allows controls to provide a stateful programming model that creates the illusion of continuity while preserving the scalability of a stateless application.

  • They contain logic to handle postback data associated with a Web request and to enable the page developer to process user input and handle user actions in their server-side code. The page developer is freed from understanding the details of postback and can instead focus on the object model of the controls ”in other words, the controls' properties and events.

  • They provide a compelling data-binding model. Most Web applications are dynamic and feature content based on a variety of data sources. Data-bound server controls ”those server controls associated with a data source ”greatly simplify the creation of dynamic pages. ASP.NET features an efficient data-binding model that provides the page developer with complete control over the data-binding process and over data access. The data-binding model features a simple and generic data source model that provides the page developer with a wide choice of objects that can be used as data sources.

  • They provide page developers with multiple mechanisms to customize their rendering. Server controls can provide style properties as a means to customize their formatting. They can also provide template properties for customization of their content and layout. Server controls use both these mechanisms to provide a lookless user interface, which is an interface that does not provide a single preestablished look but instead can blend seamlessly into the rest of the application's user interface.

  • They are configurable on a machine level (via the machine.config file) or on a Web application level (via the web.config file). Server controls can support configurable defaults for their properties, which enables page developers to control or change their behavior uniformly across pages without having to change or recompile the application itself.

  • They provide a rapid application development (RAD) user experience in a visual design-time environment such as Microsoft Visual Studio .NET. This enables page developers and designers to visually compose their pages and customize the appearance of the server controls in a WYSIWYG fashion.

In Part III of this book, "Server Controls ”Nuts and Bolts," we will provide detailed explanations of these features and characteristics. We'll also show you how to incorporate them into your own custom controls to provide an intuitive user model that is consistent with the standard ASP.NET server controls.

ASP.NET Server Control Hierarchy

The ASP.NET server control classes are implemented in the System.Web.UI , System.Web.UI.HtmlControls , and System.Web.UI.WebControls namespaces in the System.Web assembly. Figure 2-1 provides a high-level view of these namespaces.

Figure 2-1. The ASP.NET server control hierarchy

graphics/f02hn01.jpg

All server controls derive directly or indirectly from the System.Web.UI.Control class. The Page and UserControl classes represent .aspx and .ascx files, respectively, while the LiteralControl class represents a contiguous range of static content present in these files.

In the System.Web.UI.HtmlControls namespace, the HtmlControl class provides a base class for all HTML controls. These controls represent plain HTML tags with a runat ="server" attribute and provide an HTML-centric object model.

In the System.Web.UI.WebControls namespace, the WebControl class provides a base class for most Web controls and for custom controls that render themselves as HTML elements. These controls provide an object model that is strongly typed, simpler, more intuitive, and more consistent than the HTML controls.

In addition to the standard HTML-based controls, ASP.NET provides support for authoring mobile Web applications targeted at small devices such as cell phones via a suite of mobile controls in the System.Web.UI.MobileControls namespace.

ASP.NET Web Controls

The controls in the System.Web.UI.WebControls namespace are referred to as the Web controls . Most of the controls in this namespace derive directly or indirectly from the WebControl base class. This suite includes controls that render as the basic HTML form elements, such as buttons and text boxes used for collecting user input, as well as other common HTML elements, such as anchors, images, and tables. It includes a set of validation controls that can be associated with the input controls to perform both client-side and server-side validation. The suite also contains data-bound controls, such as DataGrid and Repeater , used to generate dynamic pages based on a data source. The class hierarchy presented in Figure 2-2 shows the complete set of controls.

Why Write Server Controls?

Although the standard ASP.NET server controls address the most common application scenarios, they are by no means exhaustive. In fact, many scenarios are not directly addressed by these controls. For example, the standard ASP.NET server controls do not address image maps, chart-generation capabilities, or the creation of masked-edit data entry forms. However, ASP.NET does provide an extensible control architecture that allows you to develop custom controls that can behave in the same way as the standard ones at both run time and design time. There are several reasons and scenarios for which you might develop server controls:

  • To encapsulate application logic in the form of reusable and intuitive abstractions that can then be used in multiple Web applications. In this sense, server controls can provide a toolbox of common user interface components and behaviors.

  • To create commercial component libraries similar to the ActiveX controls in Microsoft Visual Basic and the tag libraries in Java Server Pages (JSP). It is expected that the third-party component industry will make the ASP.NET platform even more compelling, just as Win32 ActiveX components popularized Visual Basic. Developing custom controls that target this space is a compelling market opportunity because Web Forms is the primary programming model for Web application development in the .NET platform.

  • To provide a clean mechanism for dividing work across a large team. For example, in corporations, developers on the team can put together controls for use by page designers who design and implement the application user interface.

  • To enable Internet service providers (ISPs) that host ASP.NET Web sites to add substantial value to their service by offering a gallery of custom server controls. This richer functionality can enable an ISP's members to create more powerful and interactive Web sites, thereby enabling the ISP to distinguish itself from other competing service providers.

Figure 2-2. The ASP.NET Web controls

graphics/f02hn02.jpg

An example of an add-on library of controls built using the control architecture in ASP.NET is the Microsoft Internet Explorer Web controls library. This library includes the DHTML-based TreeView and TabStrip controls, among others. All these controls are available for download on MSDN.



Developing Microsoft ASP. NET Server Controls and Components
Developing Microsoft ASP.NET Server Controls and Components (Pro-Developer)
ISBN: 0735615829
EAN: 2147483647
Year: 2005
Pages: 183

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net