Asp.net| Server | control | control DEVELOPMENT
In the previous series, the author has enumerated several examples of implementing custom server controls. With these examples, the reader has preliminary access to the contents of the Create server control properties. For example, implement properties such as private variables, view state, control state, and so on. Although readers can understand some of the basics of implementing attributes through these, this is not enough. Starting with this section, you will explain the issues that implement the custom server control properties. This section focuses on some basic concepts of implementing custom server control properties and basic implementation methods for simple properties.
1. Basic concepts of control properties
This section describes the basics of creating server control properties, including: (1) attribute types and forms, (2) inherited properties from Control and WebControl, and (3) design-time metadata attributes related to properties.
1 attribute type and form
Typically, server control properties can be grouped into two types: simple properties and complex properties.
A simple property is a property where the value of a property can easily be converted to a string expression, and the value of this property is typically a simple numeric type such as Boolean, Byte, Char, Double, Enum, Int32, DateTime, and string and enum types. By adding code, developers can store simple properties in the ViewState dictionary for state management between postbacks. A property is called a complex property if its type is a class that itself has a property (called a child property). For example, the type of the Font property for the WebControl class is a FontInfo class that has properties such as bold and name. Bold and name are child properties of the WebControl font property. Asp. NET page framework can save child properties on a control's opening tag by using a hyphen syntax (for example, font-bold= "true"), but if you save child properties in the markup of the control (for example,
The markup form of the attribute is discussed above, that is, the form of adding hyphens. In fact, different attributes represent different forms of markup. To deepen the understanding of simple attributes and complex attributes, here are 4 forms of markup for attributes.
· Common form Properties
This is one of the most common forms of attribute tagging. This form of attribute markup is inside the control and is defined with runat= "server". Usually the following form:
<mycontrol:customercontrol id= "Demo1" runat= "Server" propertyname= "PropertyValue"/>
Where PropertyName is a word with no hyphens. For example:
<asp:button id= "button1" runat= "Server" text= "Submit"/>
The property text here belongs to a generic form property.
· Hyphen form Attribute
The attribute in this form of markup is inside the control tag, with hyphens being the most characteristic of this form of property. In the form of:
<mycontrol:customercontrol id= "Demo1" runat= "Server" sub-propertyname= "PropertyValue"/>
Where Sub-propertyname is a word combination with hyphens. For example:
<asp:label id= "Label1" runat= "Server" font-size= "Medium" font-underline= "True"/>
In the above code, font-size and Font-underline are typical hyphen-form attributes.
· Internal nested form Attributes
Any property that has this form of markup is a complex property. It is a nested form that declares a child property of a property set inside a control tag. The form is similar:
<asp:datagrid id= "DATAGRID1" runat= "Server"
</HeaderStyle>
<footerstyle forecolor= "#330099" backcolor= "#FFFFCC"
</FooterStyle>
</asp:DataGrid>
Where HeaderStyle is an internal nested form property, ForeColor and BackColor are child properties of the HeaderStyle attribute. FooterStyle is the same as HeaderStyle, and is also an internal nested form attribute.
· Internal nested Form default property
Attributes of this form of markup are often used in the collection properties of server controls, which are necessarily complex properties. This form property is essentially the same as the markup form of the "Internal nested form attribute" described above. The difference is that when a control has this property, the control tag contains only that form property and cannot contain any other properties. That's why it's called "Default." The form 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>
Where attribute ListItem is a typical internal nested form default property.
2 inherited properties from Control and WebControl
As described in the previous article, you derive from the System.Web.UI.Control base class if you need to develop controls that do not have UI or combine other controls that render their own UI. To do this, readers should know some common properties of control classes. Table 1 lists the common properties of the control base class, which are often used during the development of server controls.
Property |
Data type |
Description |
Controls |
ControlCollection |
Gets the ControlCollection object that represents the child controls of the specified server control in the UI hierarchy |
Adapter |
ControlAdapter |
Gets the browser-specific adapter for the control. (asp.net 2.0 new) |
Apprelativetemplatesourcedirectory |
String |
Gets or sets the application relative virtual directory of the Page or UserControl object that contains the control. (asp.net 2.0 new) |
EnableTheming |
bool |
Gets or sets a value that indicates whether the theme is applied to this control. (asp.net 2.0 new) |
Page |
Page |
Gets a reference to the Page instance that contains the server control. |
Parent |
Control |
Control that belongs to its Controls collection. (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 state during a round trip. If the parent control does not maintain its view state, the view state of its child controls is not maintained automatically |
TemplateControl |
TemplateControl |
Gets or sets a reference to the template that contains the control. (asp.net 2.0 new) |
UniqueID |
String |
Unique identifier of the hierarchical qualification assigned to the control by the page frame |
ClientID |
String |
A unique identifier assigned to the control that renders as an HTML ID attribute on the client. ClientID is different from UniqueID because UniqueID can contain a colon character (:), which is not valid in the HTML ID attribute (and is not allowed in the variable name of the client script) |
Page frame
As described in the previous article, if you create a custom server control that has a UI, you should derive from any control in WebControl or System.Web.UI.WebControls, which provides the appropriate starting point for the custom control. In the same way, readers should be aware of some common properties from the WebControl class, which can be automatically inherited for controls. The properties are listed in Table 2.
Property |
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 |
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 that is rendered by the Web server control on the client. |
Enabled |
Bool |
Gets or sets a value that indicates whether the Web server control is enabled. |
EnableTheming |
bool |
Gets or sets a value that indicates whether the theme is applied to this control. (asp.net 2.0 new) |
Font |
FontInfo |
Gets the font properties associated with the Web server control. |
ForeColor |
Color |
Gets or sets the foreground color (usually the text color) of the Web server control. |
Height |
Unit |
Server Control Height |
Width |
Unit |
Server Control width |
SkinID |
String |
Gets or sets the appearance to apply to the control. (asp.net 2.0 new) |
3 Design-time metadata related to attributes
To create a server control to improve application development efficiency, each control developer wants to create a control that is like. NET Framework, the built-in standard server controls are powerful and easy to use. For example, when a control application clicks on a control in the design interface, it may want some attributes to be highlighted, some attributes to be displayed in the property browser, and so on. How do you make a control have such a feature? This involves adding the relevant design-time support code to the code.
In fact, implementing design-time metadata is a more complex subject. However, as a beginner, we do not need to grasp too deeply, the following is only a few of the common attribute-related design-time metadata settings. As shown in the code below, some design-time metadata settings and brief descriptions related to attributes are enumerated.
· bindable
This attribute indicates whether the property can be bound to a valid data source. Typically, a Boolean value is used to set, for example: bindable (True). If a property is marked with a value of true, it means that the property can bind to a valid data source, and the property change notification for the property should be raised, and if the property value is false, the property cannot bind the data.
· browsable
Specifies whether the property should be displayed in the property browser, using a Boolean setting. Typically, common properties and properties that you want to display in the property browser are set to Browsable (true), read-only properties, and properties that you do not want to see in the property browser are set to Browsable (false).
· Category
Specifies the category in which the property is grouped in the property browser. This design-time attribute helps the visual editor to group attributes logically. Usually divided into: appearance (appearance), Behavior (Behavior), Layout (Layout), data (information), Action (action), keyboard (key), Mouse (Mouse) and so on. In addition, readers can customize the taxonomy, such as category ("ItemStyle"), which indicates that the property is displayed as a ItemStyle group in the property browser.
· Description
Specifies the text description of the property that appears below the property browser. For example: Description ("This are a property").
The above content is the most common design-time metadata setting in the implementation of the property process. Whether for simple properties or complex properties should be set as needed.
· DesignerSerializationVisibility
Specifies whether and how the property is serialized in code, with an enumeration value of designerserializationvisibility. There are three ways to set up:
(1) designerserializationvisibility (Designerserializationvisibility.hidden), specifies that the serializer should not serialize the value of the property;
(2) designerserializationvisibility (designerserializationvisibility.visible), specifying that the serialization program should be allowed to serialize the value of the property;
(3) designerserializationvisibility (designerserializationvisibility.content), which specifies that the serializer should serialize the contents of the property, not the property itself. This field is read-only. It is important to note that a member without a designerserializationvisibility attribute is considered to have a value of designerserializationvisibility.visible Designerserializationvisibil ity characteristics. If possible, the serializer serializes the attribute value marked as visible to that type.
· Notifyparentproperty
Indicates that the Parent property is notified when the value of the property to which this attribute is applied is modified. In other words, if the property's parent property should be notified when the property value changes, the Notifyparentproperty attribute is applied to the property. Usually set using a Boolean value. For example, the Size property has two nested child properties: Width and height. The attribute width and height should be marked as NotifyParentPropertyAttribute (true) so that when property values change, they can notify the parent property to update its value and display it.
· ParseChildren
Use this attribute to indicate whether an XML element that is nested within a server control tag should be treated as a property or as a child control when the control is used declaratively on a page. Typically, there are two types of declarations: (1) ParseChildren (true), which indicates that the child XML element is parsed as a property of the server control, ParseChildren (false), which indicates that the child XML element is parsed as a child control of the server control ; (2) ParseChildren (bool Childrenasproperty, String defaultproperty), where the Boolean parameters in Childrenaspropety and Mode 1 are of the same meaning, Defaultproperty defines the collection properties of the server control that the child control is parsed by default.
· PersistChildren
This attribute indicates whether the child controls of the server control should be persisted as internal nested controls at design time. If the attribute is PersistChildren (true), the child controls of the server control are persisted as nested server control tags. If PersistChildren (false), the property of the control is persisted as a nested element.
· PersistenceMode
Specifies how server control properties or events are persisted to the metadata properties of the ASP.net page. Coexistence in 4 kinds of enumeration settings way:
(1) PersistenceMode (persistencemode.attribute), specifying attributes or events to remain as attributes;
(2) PersistenceMode (Persistencemode.encodedinnerdefaultproperty), specifying the property as the only internal text of the server control. The property value is HTML-encoded. This designation can only be done on strings;
(3) PersistenceMode (Persistencemode.innerdefaultproperty), which specifies that the property is persisted as internal text in the server control. It also indicates that the property is defined as the default property of the element. Only one attribute can be specified as the default property;
(4) PersistenceMode (Persistencemode.innerproperty), which specifies that the property is persisted as a nested tag in the server control. This is commonly used for complex objects; they have their own persistence attributes;
· Defaultproperty
Specifies the default properties of the server control. For example: [Defaultproperty ("MyProperty")].
· TypeConverter
Specifies the type of converter to use as the object to which this attribute is bound. The class used for conversion must inherit from TypeConverter. Use the Convertertypename property to get the class name that provides the data conversion for the object to which the attribute is bound.
2. Simple attribute Implementation Method
Some of the simple attributes are implemented in the previous articles. From this, you can find that creating simple properties can use private variables, view state, and control state. Here, I do not intend to repeat these content. Interested readers can refer to the article. This section summarizes only the procedures for implementing simple properties and is illustrated with an example that implements simple enumeration properties. The sample code looks like this:
Defining enumerations
public enum booktype{
notdefined = 0, fiction = 1, nonfiction = 2
}
Implementation Property Booktype[bindable (True), Category ("appearance"), DefaultValue (booktype.notdefined), Description ("Fiction" or Not "),]
Public virtual BookType booktype{
get {
Object t = viewstate["BookType"];
return (t = = null)? booktype.notdefined: (booktype) t;
}
set {viewstate["booktype"] = value;}
}
The above code implements an enumerated BookType (including 3 enumerated values) and a property booktype of type BookType. According to the basic concepts described above, BookType is a simple attribute. Also, this property stores the property value in the view state viewstate. With this example, we can basically summarize the implementation of simple attributes:
(1) Determining whether the attribute to be declared is a general form attribute;
(2) Determine whether the attribute value encapsulated by the property being declared is a simple numeric type, string, or enumeration type, and so on;
(3) If both steps 1 and 2 are true, the attribute to be declared is a simple property;
(4) Declaring the design-time characteristics of the property;
(5) write and read accessor code according to the design requirement of the attribute;
3. Summary
This article describes the content of creating simple properties for a custom server control using the ASP.NET 2.0 technology. With the reader's step-by-step understanding of the development of custom server controls, it will be found that implementing simple properties is a simpler and more common implementation in the process of building controls. During the creation process, readers must understand the differences between using private variables, control state, and view state. So that you can quickly and well implement simple properties.