Develop ASP.NET Custom Control (ASP.NET Learning Note 2)

zhaozj2021-02-17  49

BY CASH 12.25.2002 Learning Custom Control Not only makes you develop a more flexible system more importantly, it can deepen your understanding of the existing server control and is more flexible. Compared with the ASP, ASP.NET provides more powerful features, I prefer its code separation technology and the use of strong type languages ​​such as C #, VB.NET, which is from the developer's perspective, from From the user's point of view, it will feel that it is faster, and it is more stable, and the security is higher. In any case, the emergence of new technologies will make many people feel happy, but for developers, bad news is what they need to learn more. ASP.NET has a lot of changes. For example, you may find that the elements we operate are also very different. The original standard HTML element becomes the current server control, the so-called server control, is on the server. Run, and mapped to all browser-supported standard HTML tag controls, in your web form, all elements containing the runat = "server" attribute declared, called server controls (in the design page in VS.NET With a small green arrow), you can use the original HTML element and add the runat = "server" attribute to a server control, which is called HTMLControls, which can be directly mapped to the standard HTML tag; Brand new WebControls available to .NET can be used, it is more abstract than the former, and the function is also more powerful. Regardless of which type of control is used, the runat = "server" attribute will allow you to access these elements in programming. This is easy to understand, the server control is a control running on the server. The results of some of the server-side resolution is displayed in our browser. In fact, the Web Form can provide maximum possible browsing. The compatibility is also based on this. Some of the usual understandings, in ASP.NET, a page, a validation control, a User Control, can be seen as a separate control, which comes from the combination of other controls. In addition to using a series of controls provided by .NET, you can also find some free controls online. The Control Gallery column in www.asp.net collected a lot of common Control. In ASP.NET, (in the traditional sense) The control you develop can actually have two: Custom Control and User Control. Custom Control is longitudinally, inheritance and expansion of the system itself, in the form of a DLL file in the program, User Control is horizontally, is a combination of the system itself, in the system .ascx as a suffix name. These two control functions can provide developers with a reuseful visual UI component, compared to the former with greater flexibility, while developing is more complicated, while the latter is easy to develop, but the reusability is compared difference. Generally speaking, users will be developed with user control, and only when they are overwhelmed, use Custom Control. In fact, the user's own development of Custom Control is not a very difficult thing, just need to define a class directly or indirectly derived from the Control and rewrite its render method, System.Web.ui.Control and System.Web .Ui.webControls.webControl two classes are the base class of the server control. The Control class defines the properties, methods, and events of all server controls.

These include control controls to perform a life cycle method and event, and attributes such as ID, UNIQUEID, PARENT, ViewState, and Controls (sub-control sets). Control does not have any user interface (UI) specific features. If the created control does not provide a UI, or a control of other programs presenting its own UI, derive from Control. The WebControl class is derived from Control and provides additional properties and methods for the UI feature. These properties 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 presents a UI, derive from WebControl. You can rewrite the properties, methods, and events inherited from the base class, and you can add new properties, methods, and events to your custom control, as mentioned above, we use the rewrite render method to implement the output of the HTML code. The render method uses the System.Web.ui.htmlTextWriter type parameter. The control is sent to the client's HTML as a string parameter to the Write method of the HTMLTextWriter. Below we use vs.net to develop a simple custom control (why using vs.net is to facilitate developer debugging, we often use VS.NET in formal development). First create a new blank solution, then add two items to it, a Web Control Library item called MyControls and a web application project called Web, which is used to test the controls we have developed. Click Right click on the web project, select the dependency, so that the project Web depends on MyControls, then adds a MyControls to the web project (after the compiled myControls.dll file, by copy to the Bin folder of the Web Directory, you can Use this MyControls.dll file anywhere). Add a web custom control in the MyControls project and name myControl.cs, then add the following code to the WebForm1.aspx file in the web project (to register this control to the page): <% @ register tagprefix = "CCS "Namespace =" MyControls "Assembly =" MyControls "%> Add the following code between the

tag (add this control to the page): OK, a custom control with a text property has been completed, and now you can run it and watch the effect. The results of this control are merely output a line of text to the browser and display it, but the controls used usually used, so as to be label, large to DataGrid, is based on this principle. Then let's take a look at the composition of this custom control source file (MyControl.cs).

using System; using System.Web.UI; using System.Web.UI.WebControls; using System.ComponentModel; namespace MyControls {// Attribute DefaultProperty default attributes specified component, ToolboxData specifies IDE tool from the toolbox // Drag the default mark generated for the custom control [DefaultProperty ("Text"), ToolboxData ("<{0}: myControl Runat = Server> ")] // class MyControl derived WebControl public class mycontrol: system.Web.ui.webcontrols.WebControl {private string text; // attribute bindable Specify whether attributes are usually used to bind // category Specify attributes or events to display categories in the visual designer // defalUTValue Used to specify the default value [Bindable (TRUE), Category ("Appearance"), DefaultValue ("")] public string text {get {return text;} set {Text = value;}} // Rewrive WebControl's render method, using the HTMLTextWriter type Parameter protected override void render (HTMLTextWriter Output) {// Send the value of the property text to the browser Output.write (Text);}}} Now we have a slight change of the output value of the render method, try to add tag span: Output.Write ("" ""); you can also use tag to modify text display: Output .Write (" " text " ");

转载请注明原文地址:https://www.9cbs.com/read-28480.html

New Post(0)