Using ASP.net 2.0 technology, creating Web custom server controls is not an easy task. This requires developers to understand and have the flexibility to apply a variety of web development technologies, such as CSS style sheets, client-side scripting languages, and. NET development language, server control development technology, even the current most fire Ajax technology, and so on. Although the reality is so "difficult", but this kind of development technology is not really difficult to master. Everything has to be done from the beginning. This article will cover the basics of creating Web custom server controls using ASP.net 2.0 technology, including server control concepts, control types, lifecycle, and more.
1. What is a Web server control
In ASP.net 2.0, a Web server control is a component that executes program logic on a server. This component may generate a certain user interface, or it may not include the user interface. Each server control contains some member objects so that developers can invoke them, such as properties, events, methods, and so on.
Typically, server controls are included in the ASP.net page. When you run the page,. The net execution engine performs certain functions based on the definition of the control member object and the program logic. For example, the user interface is rendered at the client. At this point, the user can interact with the control, and when the page is submitted by the user, the control can raise the event on the server side and the server side will handle the event according to the relevant event handlers. Server controls are an important element of the WebForm programming model, and they form the basis of a new, control-based form program. In this way, you can simplify the development of Web applications and improve the efficiency of application development.
The wide application of server control simplifies the development of application and improves the work efficiency. So when should you create and use a custom server control? The following list of three specific application scenarios:
(1) An existing server control basically conforms to the application requirements, but some special features are missing, at which point you can customize the server control by deriving from the existing control and overriding its properties, methods, or events.
(2) A server control that needs to be used combines the functionality of two or more existing controls, such as a composite control that encapsulates a button and a text box. You can do this by creating a server control at this point.
(3) The existing server control (or its combination) does not meet the requirements of the application. In this case, you can create a custom server control by deriving from the base class.
2 Type of server control
ASP.net 2.0 provides a variety of server controls. Depending on how the server control is defined, it can be grouped into the following 3 types: HTML server control, ASP. NET Standard server controls and custom server controls.
(1) HTML server control
The HTML server control derives from the namespace System.Web.UI.HtmlControls. They are converted by ordinary HTML controls (which are defined by the HTML language, such as Button, input, and so on), and their rendered output is basically consistent with the normal HTML control. By default, it is difficult for the server side to control the normal HTML controls on a Web page, but developers can easily programmatically control the normal HTML controls by converting them into HTML server controls.
Converting an ordinary HTML control to an HTML server control method is relatively straightforward. In general, the conversion is done in two steps:
(1) Add the runat= "Server" attribute to the generic HTML control attribute;
(2) Set the id attribute. By converting, the associated properties, events, methods, and so on for normal HTML controls are all mapped to HTML server controls, which enables you to programmatically reference and control the HTML server control during page processing.