In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-18 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/02 Report--
This article is about how to use the control generator for ASP.NET control development. The editor thinks it is very practical, so share it with you as a reference and follow the editor to have a look.
Control generator for ASP.NET control development 1. Wrong code, unable to parse
First, let's look at a simple piece of code.
Correct < asp:Wizard ID= "Wizard1" runat= "server" > < WizardSteps > < asp:WizardStep ID= "WizardStep1" runat= "server" Title= "Step1" 21212 < / asp:WizardStep > < asp:WizardStep ID= "WizardStep2" runat= "server" Title= "Step2" > < / asp:WizardStep > < / WizardSteps > < / asp:Wizard > error < asp:Wizard ID= "Wizard2" runat= "server" > < asp:WizardStep ID= "WizardStep1" runat= "server" Title= "Step1" > 21212 < / asp:WizardStep > < asp:WizardStep ID= "WizardStep2" runat= "server" Title= "Step2" > < / asp:WizardStep > < br / > < br / > < asp:Label ID= "Label1" runat= "server" Text= "Label" > < asp:TextBox ID= "TextBox2" runat= "server" > < / asp:TextBox > < / asp:Label > < br / > < asp:TextBox ID= "TextBox3" runat= "server" > 12345 < / asp:TextBox > < br / > < asp:Label ID= "Label2" runat= "server" Text= "Label" > 12345 < / asp:Label > < br / > < br / > < asp : TextBox ID= "TextBox1" runat= "server" > < asp:Label runat= "server" Text= "Label" > < / asp:Label > < / asp:TextBox >
Wizard adds a new control to asp.net2.0. Two errors have occurred on this page, as shown in the following figure
After running this page, an error will be reported with the following prompt
Parser error in control generator for ASP.NET control development
There is a problem here, when. Net provides our control, we have formed a kind of directional thinking, the control is written that way, such as DropDownList, which can only be a set of < asp:ListItem >, then why can't I set other properties? why does the Wizard control need to add a WizardSteps property to work properly? When we think about it here, we should look for answers.
Control generator for ASP.NET control development 2. Start with the ParseChildren metadata property
Since the fifth article, we have used this feature of ParseChildren many times. Please also take a look at MSDN's explanation for it first. There are three situations.
(1) ParseChildren (true) Article 5 when we use collection properties, we have defined it this way, as follows
[ParseChildren (true)] public class Custom: Control {}
(2) ParseChildren (true, "< Default Property >") Section 10 when we defined the properties of a collection, we defined it this way.
DropItemList is a collection property
[ParseChildren (true, "DropItemList")] public class DropColor:WebControl {}
(3) ParseChildren (false), which we have not used before, is what we are going to talk about. When it is internally defined as flase, then the elements placed in this control will be parsed into controls, which should be said to be page parser ControlBuilder classes. Here you can take a look at the MSDN documentation's interpretation of the ControlBuilder class, or at least know this first.
By default, each control on the page is associated with a default ControlBuilder class.
Let's take a look at it slowly.
Control generator for ASP.NET control development 3. Control and collection properties
Let's recall the use of ParseChildren again. This time the sample code is taken from asp.net2.0 disclosure.
(1) use of ParseChildren (true, "< Default Property >")
The effect of this control is to randomly display the contents of an internal control.
RItem is a control that inherits Control. Nothing is implemented inside it. You can output and render it inside its control. Remember that the ControlBuilder class mentioned above is associated by default.
Example 1 of a control generator for ASP.NET control development
[ParseChildren (true, "Items")] public class ItemRotator: CompositeControl {private ArrayList _ items = new ArrayList (); [Browsable (false)] public ArrayList Items {get {return _ items;}} protected override void CreateChildControls () {Random rnd = new Random (); int index = rnd.Next (_ items.Count); Control item = (Control) _ items [index] This.Controls.Add (item);}} public class RItem: Control {}
Control generator page code for ASP.NET control development
< custom:ItemRotator id= "ItemRotator1" Runat= "server" > < custom:ritem ID= "Item1" runat= "server" > First Item < / custom:ritem > < custom:ritem ID= "Item2" runat= "server" > Second Item < asp:Calendar id= "Calendar1" Runat= "server" / > < / custom:ritem > < custom:ritem ID= "Item3" runat= "server" > Third Item < / custom:ritem > < / custom:ItemRotator >
Regardless of the effect, randomly display the contents of the ritem control. Note that the above control defines an Items collection property.
Another improvement is what we talked about in the tenth article, defining properties for Ritem. As a collection property, the code is no longer listed here.
(1) the use of ParseChildren (false)
This control does not add a property, but has a method AddParsedSubObject ()
Control has default page analysis logic, override the AddParsedSubObject method, you can add child controls to the control
Example 2 of a control generator for ASP.NET control development
[ParseChildren (false)] public class ContentRotator: WebControl {protected override void AddParsedSubObject (object obj) {if (obj is Content) base.AddParsedSubObject (obj);} protected override void RenderContents (HtmlTextWriter writer) {Random rnd = new Random (); int index = rnd.Next (this.Controls.Count); this.controls [index] .RenderControl (writer) }} [ToolboxItem (false)] public class Content: Control {}
Control generator page code for ASP.NET control development
< custom:ContentRotator id= "ContentRotator1" Runat= "server" > < custom:Content id= "Content1" Runat= "server" > shows the * item, which is not the second item shown by the attribute < / custom:Content > < custom:Content id= "Content2" Runat= "server" >. This is not the third item shown by the attribute < asp:Calendar id= "Calendar1" Runat= "server" / > < / custom:Content > < custom:Content id= "Content3" Runat= "server" >, it is not the attribute < / custom:Content > < / custom:ContentRotator >
Note: ContentRotator does not have any properties (controls are added internally), but uses the AddParsedSubObject method to add child controls to the control, unlike the ItemRotator control, which has properties rather than controls inside.
Control generator for ASP.NET control development 4. Modify default parsing logic
As mentioned above, each control has default parsing logic, which is implemented through the ControlBuilder class, and can be defined by overriding its methods. Here is an example that replaces a control with a custom label
Some of the codes are listed below
Example 3 of a control generator for ASP.NET control development
/ / Custom page parser public class ServerTabsBuilder: ControlBuilder {public override Type GetChildControlType (string tagName, IDictionary attribs) {if (String.Compare (tagName, "tab", true) = = 0) return typeof (ServerTab); else return null;}} [ToolboxItem (false)] public class ServerTab: Control {private string _ Text Public string Text {get {return _ Text;} set {_ Text = value;}
(1) the ServerTabsBuilder class overrides the
The GetChildControlType method gets the Type of the control type corresponding to the child tag
In this method, it rewrites the page parsing logic by replacing the ServerTab control with tab tags.
Also commonly used in the ControlBuilder class is the AllowWhitespaceLiterals method, which specifies whether white space is allowed between controls. You can rewrite this method and test it.
(2) define a simple ServerTab control.
You also need to override the AddParsedSubObject method in the parent control to add the ServerTab control to the child control
Protected override void AddParsedSubObject (object obj) {if (obj is ServerTab) base.AddParsedSubObject (obj);}
(3) * you also need to associate the control builder with the control, and of course set ParseChildren (false)
[ControlBuilder (typeof (ServerTabsBuilder))] [ParseChildren (false)] public class ServerTabs: WebControl, IPostBackEventHandler {}
All right, the main code here is realized. You can download the rendering code later. Let's take a look at the page code.
<%-- omit css code above--% > < custom:ServerTabs ID= "ServerTabs1" Runat= "Server" > < tab Text= "First Tab" > < asp:Label ID= "Label1" runat= "server" Text= "Label" > < / asp:Label > Contents of the first tab < / tab > < tab Text= "Second Tab" > Contents of the second tab < / tab > < tab Text= "Third Tab" > Contents of the third tab < / tab > < / custom:ServerTabs >
The above inlay code is tab tag, not < custom:ServerTabs > < / custom:ServerTabs >, but the effect is the same, except that we have changed the default page analysis logic and customized the control page generator (parser) to see the effect (when recompiling, you need to restart vs2005 to see the effect)
Well, this time the topic is over, it is important to note that the composite control in asp.net2.0 only needs to inherit the CompositeControl class.
Thank you for reading! This is the end of this article on "how to use the control generator for ASP.NET control development". I hope the above content can be of some help to you, so that you can learn more knowledge. if you think the article is good, you can share it for more people to see!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.