1. Basic concepts of Control Properties
This section describes the basic content about creating server control attributes, including: (1) property types and forms; (2) attributes inherited from control and webcontrol; (3) the metadata attribute associated with the attribute.
1) attribute type and form
Generally, server control attributes can be divided into two types: simple attributes and complex attributes.
A simple attribute is an attribute that can be easily converted to a string expression. The value of this attribute is generally a Boolean, byte, Char, double, Enum, int32, datetime, or other simple numeric type, and string and enumeration types. Developers can add code to store simple attributes in the viewstate dictionary for status management during sending and receiving. If the type of an attribute is a class with its own attributes (called sub-attributes), this attribute is called a complex attribute. For example, the font attribute type of the webcontrol class is a fontinfo class with its own attributes (such as bold and name. Bold and name are sub-attributes of the font attribute of webcontrol. ASP. the net page framework can save sub-attributes on the start tag of the control by using a hyphen (such as font-bold = "true") syntax with a hyphen, however, if the Sub-attributes are saved in the control tag (such as <font bold = "true">), the sub-attributes are more readable on the page.
In the previous article, we talked about the attribute markup form, that is, the form of adding a hyphen. In fact, different attributes are marked in different forms. To better understand simple and complex attributes, we will introduce four types of attributes.
· Common form attributes
This is the most common form of property tag. Property tags in this form are located inside the control and defined together with runat = "server. Usually in the following format:
<Mycontrol: customercontrol id = "demo1" runat = "server" propertyname = "propertyvalue"/>
Propertyname is a word without a hyphen. For example:
<Asp: button id = "button1" runat = "server" text = "Submit"/>
The text attribute here is a common form attribute.
· Character-connected attributes
A property in this form of markup is located inside the control tag, and a hyphen is the biggest feature of this form attribute. The format is:
<Mycontrol: customercontrol id = "demo1" runat = "server" sub-propertyname = "propertyvalue"/>
The sub-propertyname is a combination of hyphens. For example:
<Asp: Label id = "label1" runat = "server" font-size = "medium" font-underline = "true"/>
In the code above, font-size and font-underline are typical form attributes of hyphens.
· Internal nested attributes
All attributes with this tag form are complex attributes. It declares the subattributes of an attribute set in the control tag in the form of nesting. The format is similar:
<Asp: DataGrid id = "datagrid1" runat = "server">
<Headerstyle forecolor = "# ffffcc" backcolor = "#990000">
</Headerstyle>
<Footerstyle forecolor = "#330099" backcolor = "# ffffcc">
</Footerstyle>
</ASP: DataGrid>
Headerstyle is an internal nested attribute, and forecolor and backcolor are subattributes of the headerstyle attribute. Footerstyle is the same as headerstyle and is also an internal nested form attribute.
· Default attributes of internal nesting form
This type of tag attribute is usually used for the set attribute of the server control. attributes in this form must be complex attributes. This form property is essentially the same as the markup form of the "Internal nesting form property" described above. The difference is that when a control has this property, the control tag only contains this formal property and cannot contain any other property. This is why it is called "default. The format is similar:
<Asp: dropdownlist id = "dropdownlist1" runat = "server">
<Asp: listitem> 1 </ASP: listitem>
<Asp: listitem> 2 </ASP: listitem>
<Asp: listitem> 3 </ASP: listitem>
<Asp: listitem> 4 </ASP: listitem>
</ASP: dropdownlist>
The attribute listitem is a typical default attribute in the internal nesting form.
2) attributes inherited from control and webcontrol
As described in the previous article, if you need to develop controls without a UI or combine other controls that present your own UI, You can derive from the system. Web. UI. Control Base class. To this end, you should understand some common attributes of the control class. For example, Table 1 lists common properties of the control base class, which are frequently used in the development of server controls.
Attribute data type description
Controls controlcollection gets the controlcollection object, which indicates the child control of the specified server control in the UI hierarchy.
Adapter controladapter obtains the browser-specific adapter of the control. (Added in Asp.net 2.0)
Apprelativetemplatesourcedirectory string gets or sets the application of the page or usercontrol object containing the control to a virtual directory. (Added in Asp.net 2.0)
Enabletheming bool gets or sets a value indicating whether to apply a topic to this control. (Added in Asp.net 2.0)
Page page gets a reference to the page instance that contains the server control.
The parent Control belongs to the control of its controls set. (If control B is an element of a. Controls, control a is the parent of control B)
Enableviewstate bool indicates whether the control maintains its view status during the round-trip process. If the parent control does not maintain its view status, the view status of its child control is automatically not maintained.
Templatecontrol gets or sets a reference to a template containing the control. (Added in Asp.net 2.0)
Unique Identifier of the hierarchy limitation that the uniqueid string page Framework assigns to the control
The GUID assigned to the control by clientid string. The GUID is displayed as an html id on the client. Clientid and uniqueid are different because uniqueid can contain the colon character (:), which is invalid in the html id feature (and cannot be used in the variable name of the client script)
Page framework
As described in the previous article, if you create a custom Server Control with UI. web. UI. any control in webcontrols is derived. This namespace provides an appropriate starting point for custom controls. In the same way, readers should understand some common attributes from the webcontrol class, which can be automatically inherited by controls. Table 2 lists these attributes.
Attribute data type description
Backcolor color: gets or sets the background color of the Web server control.
Bordercolor color gets or sets the border color of the web control.
Borderstyle gets or sets the border style of the Web server control.
Borderwidth unit gets or sets the Border width of the Web server control.
Controlstyle style gets the style of the Web server control.
Cssclass string gets or sets the Cascading Style Sheet (CSS) class presented by the Web server control on the client.
Enabled bool gets or sets a value indicating whether to enable the Web Server Control.
Enabletheming bool gets or sets a value indicating whether to apply a topic to this control. (Added in Asp.net 2.0)
Font fontinfo: obtains the font attributes associated with the Web Server Control.
Forecolor color gets or sets the foreground color (usually text color) of the Web server control ).
Height unit server control height
Width unit server control width
Skinid string gets or sets the appearance of the control to be applied. (Added in Asp.net 2.0)
3) design-time metadata related to attributes
The server control is created to improve application development efficiency. Every control developer wants to create a control that is as powerful and easy to use as the built-in Standard Server Control in the. NET Framework. For example, when the control application clicks a control on the design interface, it may want some attributes to be highlighted, and some attributes to be displayed in the property browser. How can we make the control have such a function? In this case, you need to add relevant design support code to the code.
In fact, metadata is a complicated content during design. However, as a beginner, we do not have to master it too deeply. Below I will only explain some common metadata settings related to attributes. The code below lists some metadata settings and brief descriptions related to the attribute.
· Bindable
This feature indicates whether a property can be bound to a valid data source. You can set a Boolean value, for example, Bindable (true ). If the value true is used to mark the attribute, it indicates that the attribute can be bound to a valid data source, and a notification should be sent to change the attribute. If the attribute value is false, it indicates that the attribute cannot be bound to Data.
· Browsable
Specify whether the property should be displayed in the property browser and set it with a Boolean value. In general, the public attributes and those that you want to display in the property browser are set to browsable (true ), the read-only attributes and those that do not want to be seen in the property browser are set to browsable (false ).
· Category
Specifies the category of a property that is displayed by group in the property browser. This design-time feature helps the visual editor to logically group attributes. Usually divided into: appearance (appearance), behavior (behavior), layout (layout), data (data), Action (Action), keyboard (key), mouse (Mouse) and so on. In addition, you can also customize the category, for example, category ("itemstyle"), which indicates that the attribute is displayed as a group of itemstyle in the property browser.
· Description
Specifies the text description of the property displayed below the property browser. For example, description ("this is a property ").
The above content is the most common metadata settings during design. Both simple and complex attributes should be set as needed.
· Designerserializationvisibility
Specify whether or not the attribute is serialized in the Code. Its value is the enumerated value of designerserializationvisibility. There are three setting methods:
(1) designerserializationvisibility (designerserializationvisibility. Hidden), specifying the serialization program should not serialize the attribute value;
(2) designerserializationvisibility (designerserializationvisibility. Visible), specifying the value of the serialization property that should be allowed by the serialization program;
(3) designerserializationvisibility (designerserializationvisibility. content), specify that the serialization program should serialize the content of the attribute, rather than the attribute itself. This field is read-only. It should be noted that members without the designerserializationvisibility feature will be considered as having the designerserializationvisibility. visvisible designerserializationvisibility feature. If possible, the serialization program serializes the property value marked as visible to this type.
· Policyparentproperty
Indicates that the parent attribute will be notified when the attribute value applied to this feature is modified. In other words, if the parent attribute of an attribute should be notified when the attribute value is changed, the policyparentproperty attribute will be applied to the attribute. A boolean value is usually used for setting. For example, the size attribute has two nested sub-attributes: width and height. The property width and height should be marked as policyparentpropertyattribute (true), so that when the property value changes, they can notify the Parent property to update its value and display it.
· Parsechildren
This feature indicates whether the XML elements nested in the server control tag should be regarded as attributes or child controls when the control is declared on the page. In general, there are two declaration methods: (1) parsechildren (true), indicating that the sub-XML element is used as the Property Analysis of the server control, parsechildren (false ), the child XML element is used as the Child control of the server control for analysis. (2) parsechildren (bool childrenasproperty, string defaultproperty), where the Boolean parameter in childrenaspropety and method 1 has the same meaning, defaultproperty defines the set property of the server control that the child control is analyzed as by default.
· Persistchildren
This feature indicates whether the child control of the server control should be maintained as an internal nested control during design. If this feature is persistchildren (true), the Child control of the server control is marked as a nested Server Control. If it is persistchildren (false), the attributes of the control are kept as nested elements.
· Persistencemode
Specifies how to maintain the server control property or event to the metadata property of the ASP. NET page. There are four enumeration settings:
(1) persistencemode (persistencemode. Attribute), specifying attribute or event persistence as a feature;
(2) persistencemode (persistencemode. encodedinnerdefaultproperty), specifying properties as the unique internal text of the server control. The property value is HTML encoded. This can only be specified for strings;
(3) persistencemode (persistencemode. innerdefaultproperty), which specifies that the property is kept as internal text in the server control. It also defines this attribute as the default attribute of the element. Only one attribute can be specified as the default attribute;
(4) persistencemode (persistencemode. innerproperty), specifying the attribute to be nested in the server control. This is usually used for complex objects; they have their own persistent attributes;
· Defaultproperty
Specifies the default properties of the server control. For example, [defaultproperty ("myproperty")].
· Typeconverter
Specifies the type of the converter used as the object bound to this feature. Classes used for conversion must be inherited from typeconverter. Use the convertertypename attribute to obtain the class name that provides data conversion for the object bound to this feature.
2. Simple attribute Implementation Method
Some simple attribute implementation methods have been introduced in the previous articles. You can find that you can use private variables, view statuses, and control statuses to create simple properties. I have no intention to repeat these contents here. Interested readers can refer to related articles. This section only summarizes the process of implementing simple attributes and describes how to implement simple enumeration attributes. The sample code is as follows:
// Define Enumeration
Public Enum booktype {
Notdefined = 0, fiction = 1, nonfiction = 2
}
// Implement the attributes booktype [Bindable (true), category ("appearance"), defaultvalue (booktype. notdefined), description ("fiction or not"),]
Public Virtual booktype {
Get {
Object T = viewstate ["booktype"];
Return (t = NULL )? Booktype. notdefined: (booktype) T;
}
Set {viewstate ["booktype"] = value ;}
}
The above code implements an enumerative booktype (including three enumerated values) and an attribute booktype with a type of booktype. According to the Basic concepts described above, booktype is a simple attribute. In addition, this property stores the attribute values in the view State viewstate. Through this example, we can summarize the implementation methods of simple attributes:
(1) Determine whether the attribute to be declared is a common formal attribute;
(2) Determine whether the attribute value to be declared is a simple numeric type, string type, or enumeration type;
(3) If steps 1 and 2 are true, the attribute to be declared is a simple attribute;
(4) Declare the design-time characteristics of this attribute;
(5) write the reader code according to the design requirements of the attribute;
3. Summary
This article describes how to create simple attributes for custom server controls using ASP. NET 2.0 technology. As readers gradually understand the development of custom server controls, they will find that the implementation of simple attributes is a relatively simple and common implementation content in the process of building controls. During creation, you must understand the differences between private variables, control statuses, and view statuses. In this way, simple attributes can be implemented quickly and effectively.
This article from the csdn blog, reproduced please indicate the source: http://blog.csdn.net/zj1103/archive/2009/04/05/4049991.aspx