Summary of ASP. NET Control Development 1.1 where to inherit
Custom Controls generally use the following base classes (data controls are not included here)
1. Control class (the base class of all server controls is a relatively underlying class. If the Control function is relatively simple and does not have many requirements, you can directly inherit this class .)
2. WebControl class (the base class of a standard control, which inherits from this class. You can inherit from its rich public attributes. If the control in the standard control does not have the control you need, you can inherit from this class)
3. CompositeControl class (class added in 2.0, which inherits from the WebControl class. If you need to create a composite control, start with inheriting this class)
4. inherit directly from built-in controls (we know that the wheel only needs to be invented. If your requirements are similar to those of built-in controls, please consider doing so)
Summary of ASP. NET Control Development 1.2 display controls
The Control class Render method is the basic rendering method. The RenderContent method is based on the Render method and adds a label to the Control. I think the RenderContent method is prepared for attributes in the WebControl class. the basic methods continue to be deeply understood.
Summary of ASP. NET controls 1.3 interaction with users
Purely rendering controls are not enough. We need to pass data, which will inevitably let us know how to process custom control events.
Summary of ASP. NET Control Development 1.4 contact attributes
Attributes are relatively simple and easy to understand, but difficult. When there are too many control attributes, it is easy to mess up, so we know the metadata and classify the attributes, such
To make it clearer, we divide attributes into good categories. In addition, we define multiple attributes of the same type in a large attribute, which is called a complex attribute, such
There are already enough attributes, so we know that each type of the attribute may be different. After rendering, the attributes are in the string format and simple attributes. net has processed the conversion for you. After you customize complex attributes, You need to define a type converter for your custom complex attributes.
Another way is to use attributes as set attributes. it can be said that it is a special complex control, which can be provided for some list controls (see article 10th). Attribute usage is quite a lot.
Summary of ASP. NET Control Development 1.5 control style
We naturally need a Style for a widget. Here, we once again recognize the WebControl class. Of course, we also know the Style class and its subclass.
As long as you understand several of the methods, you can customize the control style.
Summary of ASP. NET Control Development 1.6 Composite Control
Composite controls are often powerful. They use existing controls to assemble them into a new one. In this process, we learned to define events and styles in composite controls.
Summary of ASP. NET Control Development 1.7 view status
A topic to be discussed
Generally, the attributes defined for the control are saved as view states. However, you need to know how to customize view states for the definition of complex attributes and control style definitions.
Finally, we also discussed how to add the client function for the control, as well as the definition generator for the control. well, the conclusion finally came to an end. I wrote the above two times. The data was lost after the first write operation.
Below we will share some tips, which you may already know. for better learning in the future, I will change all the naming controls to AspDemo. CustomComponents.
The source code for this download includes 1-16 sample codes. If you have any errors, please note that
Summary of ASP. NET control development:
Summary of ASP. NET control development I. Use of embedded Resources
1. Embedded Control icon
Built-in controls all have their own icons. Many people also like to create icons for their own controls. How can this problem be solved?
You only need to use the ToolboxBitmap metadata. You need to understand its constructor. Its usage is as follows:
- namespace AspDemo.CustomComponents
- {
- [ToolboxBitmap(typeof(ImageControl), "Resources.Image.bmp")]
- public class ImageControl : WebControl
- {
- }
- }
Note:
(1) the icon is located. Use your default namespace as the root directory and use the dot syntax to specify the icon position (that is, the icon path is/Resources) otherwise, you can name the control as the root directory (the icon path is CustomComponents/Resources ).
(2) On the property panel, specify the icon file as an embedded resource, as shown in figure
2. Embed other resource files
I remember that we used to create a control and needed a js file. When we needed this control, we also needed to use the js file. This would be very difficult for others to use, we can use js files as embedded resources to solve this problem. the following example is from MSDN. Here we only show you how to use it.
- [Assembly: WebResource ("AspDemo. CustomComponents. Resources.
- AspDemo.CustomComponents.ResourceLabel.image1.jpg","Image/jpeg")]
- [Assembly: WebResource ("AspDemo. CustomComponents. Resources.
- AspDemo.CustomComponents.ResourceLabel.help.htm","Text/html",
- Required msubstitution =True)]
- NamespaceAspDemo. CustomComponents
- {
-
- Public ClassResourceLabel: Control
- {
- Protected Override VoidCreateChildControls ()
- {
-
- // Create a new Image control.
- Image _ img =NewImage ();
- // Obtain resource file reference
- _ Img. ImageUrl =This. Page. ClientScript. GetWebResourceUrl (This. GetType (),
- "AspDemo. CustomComponents. Resources. AspDemo.
- CustomComponents.ResourceLabel.image1.jpg");
- This. Controls. Add (_ img );
-
- // Create a new Label control.
- Label _ lab =NewLabel ();
- _ Lab. Text ="A composite control using the WebResourceAttribute class .";
- This. Controls. Add (_ lab );
-
- // Create a new HtmlAnchor control linking to help.htm.
- HtmlAnchor a =NewHtmlAnchor ();
- A. HRef =This. Page. ClientScript. GetWebResourceUrl (Typeof(ResourceLabel ),
- "AspDemo.CustomComponents.Resources.AspDemo.CustomComponents.ResourceLabel.help.htm");
- A. InnerText ="Help link";
- This. Controls. Add (NewLiteralControl ("</Br>"));
- This. Controls. Add ();
-
- }
- }
- }
Note:
(1) Same as the 2.1 above
Specifies the file name)
OK.
Summary of ASP. NET control development 2. Persistent control status
Only part of the code (from MSDN) is provided below. I believe there are a lot of such information.
- protected override void OnInit(EventArgs e)
- {
- base.OnInit(e);
- Page.RegisterRequiresControlState(this);
- }
-
- protected override object SaveControlState()
- {
-
- object obj = base.SaveControlState();
-
- if (indexValue != 0)
- {
- if (obj != null)
- {
- return new Pair(obj, indexValue);
- }
- else
- {
- return (indexValue);
- }
- }
- else
- {
- return obj;
- }
- }
-
- protected override void LoadControlState(object state)
- {
- if (state != null)
- {
- Pair p = state as Pair;
- if (p != null)
- {
- base.LoadControlState(p.First);
- indexValue = (int)p.Second;
- }
- else
- {
- if (state is int)
- {
- indexValue = (int)state;
- }
- else
- {
- base.LoadControlState(state);
- }
- }
- }
- }
Summary of ASP. NET control development 3. client callback
ASP. NET Unleashed separately lists the use of JavaScript in custom controls. I 'd like to give you an example here. I personally feel very good and very easy to understand. if you learn this, you can also understand several controls in the AtlasControlToolkit, almost using this technology.
Summary of ASP. NET control development 4. Configuration File
(1) pre-define control labels and register controls in web. config so that you can save
Use the @ Register command
- ﹤pages﹥
- ﹤controls﹥
-
- ﹤add tagPrefix="aspDemo" namespace="AspDemo.CustomComponents" assembly="AspDemo.CustomComponents"/﹥
- ﹤/controls﹥
-
- ﹤/pages﹥
(2) control ing
The URL can be mapped, or the control can. This method is used when configuring the Ajax environment.
- ﹤pages﹥
- ﹤tagMapping﹥
- ﹤add tagType="System.Web.UI.WebControls.RequiredFieldValidator"
- mappedTagType="System.Web.UI.Compatibility.RequiredFieldValidator,
- System.Web.Extensions, Version=1.0.61025.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35"/﹥
- ﹤/pages﹥
5. Hide controls on the toolbar
Let's talk about another small thing. When the defined control does not need to be displayed on the toolbar, you can use this metadata to hide the control.
- [ToolboxItem(false)]
- public class ImageControl : WebControl
- {
- }
Well, I have written this article. If you have any errors, please point out that the above is based on experience.
The summary of ASP. NET Control Development basics and the summary and supplement of ASP. NET Control Development basics are introduced here. I hope to learn more about ASP. NET Control Development basics.
- Development of ASP. NET controls: controls set attributes
- Custom view State Management Based on ASP. NET control development
- Add styles for child Controls Based on ASP. NET controls
- Server Control client Function Based on ASP. NET control development
- Analysis on control generator of ASP. NET Control Development