From using a basic text editor to creating a tag page, web development has gone through a long process. Currently, the integrated development environment (IDE) provides graphical representation for almost every aspect of the development process. In addition, descriptive programming techniques are implemented to improve efficiency and reduce the chance of errors. Visual Studio 2005 and ASP. the control architecture in net 2.0 follows these programming trends and provides a reliable and scalable environment designed to allow developers to create controls that can be configured in a descriptive manner.
In addition, the new adaptive rendering model in ASP. NET reduces the need to write controls that can specifically identify the target browser. In other words, control developers can focus on designing controls, while ASP. NET Framework converts controls and presents them for different types of browsers and devices.
Although ASP. NET 2.0 provides the incremental improvement feature during the control design process, the actual control rendering model has been completely changed. As a custom control developer, you will see several new options using ASP. NET. Most importantly, you will find that you can complete the same task by writing less code.
In ASP. NET 2.0, there are many methods for creating custom server controls. Each method has its advantages and limitations. This article describes how to create and configure custom controls. Code examples and architecture concepts require a moderate understanding of the C # programming language.
Adaptive rendering Model
In ASP. NET 1.x, custom control developers must design each server control so that it can identify different browser types and generate correct output. ASP. the net 1.x control framework provides several functions to make the task easier. However, developers must write the switching program based on the browser type and develop the appropriate HTML, then test the controls for different types of browsers. In addition, if a developer wants to display a widget on a mobile device, he must create a new control that is different from the one used on a common web browser.
ASP. NET 2.0 simplifies browser detection and rendering through a new adaptive rendering model. The adaptive rendering model introduced in ASP. NET 2.0 is designed to support various devices that can use tag formats (including HTML, WML, XHTML, or chmtl.
Adaptive Presentation Model Architecture
Each control can be linked to an adapter, which modifies the behavior and flag of the control for a specific target device. For example, the HTML adapter generates ASP. NET controls as standard HTML and DHTML for normal Web browsers. On the other hand, the WML adapter converts the same control into a wireless markup language for use by cell phones or other mobile devices.
Figure 1. Control-adapter Life Cycle
Describes the one-to-one ing between the control method and the adapter method. If there is an adapter (if the adapter property of the control is not empty), the execution will be transmitted between the control and the adapter method, as shown in. In the generation phase, both the control object and the adapter object can generate the output (usually both generate the output at the same time ). In general, if there is an adapter, the implementation of the adapter will overwrite the implementation of the control. In ASP. NET 2.0, the adaptive rendering model applies to all ASP. NET controls (not just mobile controls) and allows ASP. NET 2.0 to support a unified control architecture.
Practical Significance
The adaptive rendering model has two main aspects. First, as a developer, you can design the control at a time and expect it to be available on any type of device or browser with an adapter. Second, you can use a wide range of Microsoft tests for commonly used adapters to reduce your browser-specific tests.
The adaptive rendering model also provides ASP. NET 2.0 with the opportunity to add other services to the control generation process. With the adapter model, you can:
1) Use a filter to change the widget's appearance based on the target type.
2) use a template to change the layout of the entire page based on the target type.
3) control the rendering on the browser based on the browser, instead of relying on the uplevel/downlevel of ASP. NET 1.x.
In this article, we will focus on creating applications for custom controls. However, remember that the adaptive rendering model is a new basic framework.
Create a custom Server Control
Visual Studio 2005 provides many useful tools for developing custom server controls. To illustrate some features, we will create a maillink control that exposes two attributes: email and text. This control will generate the necessary HTML to wrap the provided text into the mailto: link tag.
Create a project
In Visual Studio 2005, we create a new "Web control library" project by selecting the appropriate icon in the new project Wizard:
Figure 2. New Project Wizard in Visual Studio 2005
This project is created using the default custom control class. For our example, we will rename the default file as maillink. CS.
Note: When you rename the file in Solution Explorer, Visual Studio 2005 automatically updates the class name.
The source code of maillink is built on the default template generated by the Project Wizard. The maillink class is automatically derived from the webcontrol base class.
Public class maillink: webcontrol {
The webcontrol class provides default implementation methods, which can be easily overwritten to provide detailed descriptions for our controls.
Add attribute
In the maillink example, we need to add the email and text attributes. To correctly configure these attributes, we must not only write code, but also assign several features.
[Bindable (true ),
Category ("appearance "),
Defaultvalue (""),
Description ("the e-mail address.")]
Public Virtual string email {
Get {
String S = (string) viewstate ["email"];
Return (S = NULL )? String. Empty: S;
}
Set {
Viewstate ["email"] = value;
}
}
Features (in bold) define how the new control interacts with the designer (Visual Studio. The features of the email attribute tell Visual Studio how to process the attribute during the design process:
1) Bindable-Email attributes can be bound to the data source. You can link the email field to a database, XML file, or any other dataset. This feature forces Visual Studio to display the email attribute in the controllable property list.
2) The appearance-Email attribute is displayed in the attribute view under the appearance category. You can select any expected category, including the default category: appearance, accessibility, behavior, Data, layout, or Misc. If you select the category organization method of the attribute, the email attribute is displayed under appearance.
3) The defaultvalue-Email attribute has an empty default value. Although the null value is meaningful for the email field, it may not be suitable for other attributes you add to the control. When users place your controls on their web pages, selecting the appropriate default value can save users with countless click operations.
4) Description-attribute description is displayed in the control list and may also appear as a tool prompt. The email attribute will have the email address description.
5) localizable-It notifies ASP. NET 2.0 Framework by sending signals. This control includes text attributes that can be configured for different languages or locations.
You can use the features in the system. componentmodel namespace to further improve the appearance and behavior of any special attributes. In the use designer section of this article, we will introduce in detail how to modify attributes or controls.
Next, we need to add the text attribute. The text attribute is slightly different from the email attribute, because we want to display the text as part of the HTML sent by the maillink control. Therefore, we need to add a new feature from the system. Web. UI namespace.
[Bindable (true ),
Category ("appearance "),
Defaultvalue (""),
Description ("the text to display on the link ."),
Localizable (true ),
Persistencemode (persistencemode. innerdefaultproperty)]
Public Virtual string text {
Get {
String S = (string) viewstate ["text"];
Return (S = NULL )? String. Empty: S;
}
Set {
Viewstate ["text"] = value;
}
}
The persistencemode (persistencemode. innerdefaultproperty) feature (bold Code) of the text attribute specifies that the designer should serialize the property as internal content within the control tag. This feature also states that text is the default property of the control. When you use this control in Visual Studio, the text attribute is automatically displayed on the graphic designer as the internal text of the control, if you click the control and try to change the displayed text, the text attribute is automatically changed.
On the other hand, the features applied to properties will affect the interaction between users and controls during design. These features are ignored when ASP. NET is running.