The basic concepts of control style properties are described in the previous article, and the methods for overriding style properties are illustrated by examples. This article focuses on the method of creating typed style attributes.
Ways to implement typed style properties
Classes that inherit from the style class are called typed styles. Style classes can be extended by control developers, creating a custom typed style that overrides or adds properties of the style class. Server controls can also use custom typed styles as the type of the ControlStyle property. For example, the ControlStyle property of a Table control is the TableStyle type, which is an extended style that adds such as cellpadding, cellspacing, and gridlines properties. After preliminary understanding of the basic concepts of typed style properties, the following is an outline of how to implement typed style properties.
(1) Create a class derived from System.Web.UI.WebControls.Style;
(2) Defines the properties that the style will provide for the control. Save the property in the ViewState dictionary of Style;
(3) Rewriting the CopyFrom and Mergewith methods, copying from the defined properties or merging the defined attributes with the properties of a given style;
(4) Rewrite the Reset method to remove the attributes added to the ViewState;
(5) Rewrite the AddAttributesToRender method to produce HTML and CSS attributes as part of the control rendering process.
In fact, creating a typed style property is not a straightforward process. To do this, we will use the typical application examples to illustrate the specific methods of creation, so that readers can deepen their understanding of the implementation essentials.
Typical applications
This section explains how to implement and use a custom typed style by creating a Mypanel control and associated typed style mypanelstyle. In terms of functionality, Mypanel is consistent with the ASP.net 2.0 built-in panel control. Developers can add controls to the Controls collection by nesting the controls that need to be added to the label of the control. In the visual designer, you can add controls to the Controls collection by dragging and dropping the controls you want to add to the design interface of the panel. However, Mypanel does not inherit from the Panel class, but rather the result of a custom implementation, and the control also provides a typed style property myPanelStyle with 3 style properties set:
(1) Backimageurl, used to specify the URL of the background picture;
(2) HorizontalAlign, used to specify the level of content to add to its way;
(3) Wrap that specifies whether to allow wrapping of the added content.
The example effect diagram is listed below.
Figure 1
As shown in Figure 1, the figure shows a Mypanel control that includes a line of text, the background image of the text is defined, and the text is centered.