Learning to develop custom controls not only enables you to develop a more flexible system, but also enables you to deepen your understanding of existing server controls and make your applications more flexible.
Asp.net provides more powerful functions than ASP.CodeSeparation Technology and. from the developer's point of view, from the user's point of view, it will feel faster, more stable, and more secure. In any case, the emergence of new technologies will always make many people happy, but for developers, the bad news is that they need to learn more new knowledge.
Asp.net has changed a lot. For example, you may find that the elements for our operation are also quite different from those in the past. The original standard HTML elements have become the current server controls, the so-called server control is to run on the server and map it to the standard HTML-tagged controls supported by all browsers. In your web forms, any element that contains the runat = "server" attribute declaration is called a server control (in. the design page in. NET is represented by a small green arrow.) You can use the original HTML element and add the runat = "server" attribute to make it a server control. This is called htmlcontrols, it can be directly mapped to standard HTML tags. NET provides a brand new webcontrols, which is more abstract and powerful than the former. No matter which type of control is used, the runat = "server" attribute will allow you to access these elements programmatically.
This is easy to understand. A server control is a control that runs on the server.ProgramThe standard HTML code generated after parsing is displayed in our browser. In fact, the reason why Web form can provide the most possible browser compatibility is also based on this. In Asp.net, a page, a verification control, and a user control can be considered as an independent control, it comes from the combination of other controls. In addition to a series of controls provided by. net, you can also find some free controls on the Internet. The control gallery column in www.asp.net collects many commonly used controls.
In Asp.net, there are actually two types of controls available for you to develop: custom control and user control. Custom Control is a vertical control that inherits and extends the control of the system. It exists in the form of DLL files in the program, while user control is a horizontal control that combines the control of the system, in the system. ascx is the suffix. The two controls share the same functions and can provide reusable visual UI components for developers. In contrast, the former has greater flexibility and is more complex for development, while the latter is easy to develop, however, the reusability is relatively poor. Generally, users will first use user control for development, and use custom control only when it is not reusable.
In fact, it is not very difficult for users to develop custom control by themselves. They only need to define a class derived directly or indirectly from control and override its render method, system. web. UI. control and system. web. UI. webcontrols. the webcontrol class is the base class of the server control. The control class defines the attributes, methods, and events that all server controls share. These include methods and events that control the execution of the control, and attributes such as ID, uniqueid, parent, viewstate, and controls. Control does not have any function specific to the User Interface (UI. If the created control does not provide a UI, or combines other controls that present its own UI, it is derived from control. The webcontrol class is derived from control and provides additional attributes and methods for UI functions. These attributes include forecolor, backcolor, Font, borderstyle, height, and width. Webcontrol is the base class of the Web Server Control series in ASP. NET. If the control renders the UI, it is derived from webcontrol.
You can override attributes, methods, and events inherited from the base class, and add new attributes, methods, and events to the custom control. As described above, we use the rewrite render method to output HTML code. The render method uses system. Web. UI. htmltextwriter parameters. The control sends the HTML to the client as a string parameter to the write method of htmltextwriter. Next we will use. net to develop a simple custom control (using. net is used to facilitate debugging by developers.. net ).
First, create a blank solution, and then add two projects to it, one web control library project named mycontrols and one web application project called Web, the latter is used to test the developed controls.
Right-click a web project and select a dependency so that the project web depends on mycontrols. Then, add a reference to mycontrols for the web Project (Compiled mycontrols. after the DLL file is copied to the bin folder in the web directory, you can use this mycontrols. DLL file ).
Add a web custom control in the mycontrols project and name it mycontrol. cs. Then add the following code to the first line of the webform1.aspx file in the web project (used to register this control with the page ):
<% @ Register tagprefix = "CCS" namespace = "mycontrols" assembly = "mycontrols" %>
Add the following code (add this control to the page) between the <form> tag ):
<CCS: mycontrol id = "control1" runat = "server" text = "Hello World"> </CCS: mycontrol>
OK. A custom control with the text property has been completed. Now you can run it and view the effect.
The result of this control is only to output a line of text to the browser and display it, but the control we usually use is as small as label, as big as DataGrid, developed based on this principle. Next, let's take a look at the composition of the custom control source file (mycontrol. CS.
Using system;
Using system. Web. UI;
Using system. Web. UI. webcontrols;
Using system. componentmodel;
Namespace mycontrols
{
// Attribute defaultproperty specifies the default attribute of the component. toolboxdata specifies that
// The default tag generated when the custom control is dragged in the toolbox
[Defaultproperty ("text "),
Toolboxdata ("<{0}: mycontrol runat = Server> </{0}: mycontrol>")]
// Class mycontrol derived from webcontrol
Public class mycontrol: system. Web. UI. webcontrols. webcontrol
{
Private string text;
// Attribute Bindable specifies whether the attribute is usually used for binding
// Category the specified property or event will be displayed in the category of the visualization designer
// Defalutvalue is used to specify the default value of the attribute.
[Bindable (true ),
Category ("appearance "),
Defaultvalue ("")]
Public String text
{
Get
{
Return text;
}
Set
{
TEXT = value;
}
}
// Rewrite the render method of webcontrol and use parameters of the htmltextwriter type.
Protected override void render (htmltextwriter output)
{
// Send the property text value to the browser
Output. Write (text );
}
}
}
now let's slightly modify the output value of the render method, and try to add the label span to the output text:
output. write ("" + TEXT + "");
you can also use labels to modify the display of text:
output. write (" " + TEXT + " ");
You can also add more attributes to control the output of text. On this basis, you can create rich UI controls.
sometimes we may develop server controls for different projects, such as DataGrid for special purposes. Sometimes we may be asked to develop some common controls, for example, general controls such as chart, Treeview, and menu. For this purpose, you may have four different options:
create a user control that encapsulates the server control of its user interface (UI) without writing any additional code.
Develop a compilation control that combines the functions of two or more existing controls. For example, you need a control that encapsulates a button and a text box. You can use the composite control.
derive from an existing control and override its attributes, methods, or events to customize the existing control.
create a custom control from one of the basic control classes.
the above four methods are complex in order of difficulty. As described above, the principle of use should be considered only when the former fails to meet the project requirements. Generally, the programming model used to develop user controls is very different from the last three. It is more similar to ASP. NET page development.
This article briefly describes the definition and usage of custom controls, and uses. net actually developed a custom control (used to display a string of text fields). You can use it in your project, you can also publish it to the Internet for people to download and use. Of course, the function of the control mycontrol is not enough to attract users. Next we will discuss some advanced topics about custom controls, this includes defining attributes, maintaining status, processing and sending back data, binding templates to data, and so on to improve its availability.