Using ASP. NET 2.0 technology to create Web custom server controls is not easy. This requires developers to understand and flexibly apply a variety of Web development technologies, such as CSS style sheets, client scripting languages, and ,. NET development language, ASP. NET 2.0 Server Control development technology, or even the most popular AJAX technology. Although the reality is so "difficult", this development technology is not really difficult to grasp. Everything should start from scratch. This article will introduce in detail the basic knowledge of creating Web custom server controls using ASP. NET 2.0 technology, including the concept of server controls, control types, and lifecycle.
1. What is a Web server control?
In ASP. NET 2.0, Web server controls are components that execute program logic on the server. This component may generate a certain user interface, or it may not include the user interface. Each Server Control contains some Member objects for developers to call, such as attributes, events, and methods.
Usually, ASP. NET 2.0 Server controls are included in ASP. NET pages. When you run the page, the. NET execution engine performs certain functions based on the control member object and Program Logic definition. For example, the user interface is displayed on the client. At this time, the user can interact with the control. When the page is submitted by the user, the control can trigger an event on the server side, and the server side can process the event according to the relevant event processing program. Server controls are an important element of the WebForm programming model. They constitute the foundation of a new control-based form program. This method can simplify the development of Web applications and improve the development efficiency of applications.
The extensive application of server controls simplifies application development and improves work efficiency. So when should I create and use a custom Server Control? The following lists three specific application scenarios:
1) An existing Server Control basically meets the application requirements. However, if some special functions are missing, you can customize the server control by deriving from the existing control and rewriting its properties, methods, or events.
2) The server control to be used combines the functions of two or more existing controls, for example, encapsulating a button and a textbox compound control. In this case, you can create a server control.
3) Existing server controls or their combinations) do not comply with application requirements. In this case, you can create a custom Server Control by deriving from the base class.
2. Server Control Type
ASP. NET 2.0 Server controls provide multiple types. Server controls can be defined in the following three types: HTML server controls, ASP. NET standard server controls, and custom server controls.
1) HTML server controls
The HTML Server Control is derived from the namespace System. Web. UI. HtmlControls. They are converted from common HTML controls that have been defined by the HTML language, such as buttons and Input. The output is basically the same as that of common HTML controls. By default, it is difficult for the server to control common HTML controls on Web pages. However, by converting common HTML controls into HTML server controls, developers can easily control programming.
It is easy to convert a common HTML control to an HTML Server Control. Generally, the conversion is completed in two steps:
1) Add the Runat = "Server" attribute to the features of common HTML controls;
2) set the ID attribute. Through conversion, the attributes, events, and methods of common HTML controls are all mapped to the HTML server controls, by programming, You can reference and control the HTML Server Control During page processing.
HTML server controls have the following important features:
1. You can use object-oriented technology to control programming on the server, which facilitates programming and development.
2. automatically maintain the view status. During the round-trip from the page form to the server, the value entered by the user in the HTML server control will be automatically maintained in the page return.
3. interact with the verification control to verify whether the user has entered the appropriate information in the control.
4. Allow custom attributes in the HTML Server Control. Developers can add any required attributes to the attribute set of the HTML server control. The page framework will read and present them without changing any other functions.
2) ASP. NET standard server controls
ASP. NET standard server controls are defined in the namespace System. Web. UI. WebControls. The so-called "standard" means that such server controls are embedded in the ASP. NET 2.0 Framework and are pre-defined. These controls are not mapped to HTML server controls one by one. They have richer functions and are more abstract than HTML server controls.
Compared with ASP. NET 1.x, ASP. NET 2.0 adds more than 50 standard server controls. According to the functions provided by the control, ASP. NET standard server controls can be divided into the following six types:
1) standard controls: They mainly refer to traditional Web form controls, such as TextBox, Button, and Panel controls. They have a set of standardized attributes, events, and methods, making development easier.
2) Data Control: This type of control can be subdivided into two types: Data Source Control and Data Binding control. The data source control mainly implements data source connection, SQL statement/stored procedure execution, and return data sets. Including SqlDataSource, AccessDataSource, XmlDataSource, SiteMapDataSource, and ObjectDataSource. Data Binding controls include Repeater, DataList, GridView, DetailsView, and FormView. These controls are used to display data, provide editing, deletion, and other user interfaces. Generally, you need to use the data source control to connect to the database and return a data set. Then, use the data binding control to display, update, and delete data. With the powerful support of Visual Studio 2005, developers can quickly implement the above functions without writing a line of code.
3) verification controls: these are special controls that contain verification logic to test user input. These include: RequiredFieldValidator, RangeValiedator, RegularExpressionValidator, and CompareValidator. Developers can append the verification control to the input control to test the content that users enter for the input control. The verification control can be used to check input fields and test against specific values or modes of characters to verify whether a value is within a limited range or other logic.
4) site navigation controls: these controls can be combined with site navigation data to implement site navigation functions. Including Menu, SiteMapPath, and TreeView. For large websites, site navigation controls have broad application prospects.
5) WebParts control: Web components are an amazing feature that allows you to create Web applications with highly personalized features. The WebParts control is required to implement the Web part function. ASP. NET 2.0 provides the following controls, such as WebPartManager, WebPartZone, EditorZone, CatalogZone, PageCatalogPart, and AppearanceEditorPart.
6) logon controls: these controls can quickly implement user logon and related functions, such as displaying logon status, password recovery, and creating new users. Including: LoginView, Login, CreateUserWizard, and LoginStatus.
ASP. NET standard server controls are officially provided, so they are provided with strong support within the system. For developers, these controls are the main force in building Web applications.
3) custom ASP. NET 2.0 Server controls
The custom Server Control is derived from the namespace System. Web. UI. Control or System. Web. UI. WebControls. This type of server control is completely designed and developed by developers. developers can customize the UI, functions, attributes, methods, events, and other features. This is a custom Server Control and ASP. NET standard server controls.
Common Custom server controls include composite controls, validation controls, template controls, and data binding controls.
1) composite controls: these controls contain two or more existing controls. It reuse the implementation provided by sub-controls for control rendering, event processing, and other functions.
2) verification control: the verification control definition is the same as that in the Standard Server Control described above.
3) template control: This type of control provides a common function called template. The template control does not provide a user interface, but is provided through an inline template. This means that the template control allows page developers to customize the user interface of the control.
4) data binding control: the data binding control definition is the same as that in the Standard Server Control described above.
In addition to the preceding four types of controls, custom server controls have the following features:
1) high flexibility: developers can customize the UI, functions, attributes, methods, and events based on application needs.
2) style support: the custom server control may be derived from System. web. UI. webControls. Therefore, you can use the inherited Style attributes to define a Style, such as font, height, width, and color.
3) provides extended functions for standard server controls: Custom server controls can be used to extend or improve related properties, methods, and functions on the basis of inheriting standard server controls, you can even combine different server controls to form a composite control.
4) Ease of deployment: it has the "plug-and-play" feature. Developers only need to copy the compiled custom server control to the relevant bin directory.
5) difficult to create: developing custom server controls requires developers to be proficient in many aspects of technology and requires a lot of effort and time.