With. NET technology continues to develop and mature, server control is more and more favored by the vast number of developers. At the same time, the development of server control also presents some trends, for example, more and more powerful functions, many functions gradually from the server side to the client. In addition, server controls are becoming more attractive. This may be related to the increasingly functional and outward-looking development ideas of the Windows operating system. This article and its subsequent articles will focus on how to achieve a beautiful appearance for server controls. In fact, the appearance of a server control is primarily determined by style properties. This article focuses on the implementation of the control style of the basic knowledge of the general explanation.
Introduction to server Control styles
For ordinary application developers, you only need to know what style properties The server control has, and how each style property might affect the appearance of the control. For example, if you need to modify the page background color, you need to modify the Style property BackgroundColor value, and if you need to set the appearance of the Table object, you may need to set style properties such as BorderColor, BorderWidth, and so on. However, for a control developer, not only do they need to master the knowledge that the application developer has, but they must also understand how to build control style properties.
Typically, server controls that have style properties inherit from the System.Web.UI.WebControl base class. In this way, the control class can automatically inherit multiple style properties from the base class. These style properties include getting or setting the BackColor of the control's background color, getting or setting the forecolor of the control's foreground color, getting or setting the bordercolor of the control's border color, getting or setting the control's border style BorderStyle, and so on. If the control class inherits from the WebControl base class, these style properties are automatically inherited and allow developers to override these style properties as appropriate. In addition, if the control class inherits from other existing control classes, such as the GridView, the custom control automatically inherits the style properties of the GridView base class, such as setting the AlternatingRowStyle for alternating data row styles, Sets the style of the data row being edited EditRowStyle and so on. Obviously, the style attributes inherited from the existing server control are not the focus of the discussion here. However, readers should understand that style attributes are allowed to inherit from the base class and can be used directly without modification. The style properties in the WebControl class continue to be discussed below.
The styles of the WebControl class are encapsulated in the ControlStyle property. The property value is the style data type. To better understand ControlStyle, the following is an enumeration of the ControlStyle attribute's definition code.
private Style _controlStyle;
//定义ControlStyle属性
public Style ControlStyle{
get {
if(_controlStyle == null) {
_controlStyle = CreateControlStyle();
if(IsTrackingViewState) {
((IStateManager)_controlStyle).TrackViewState();
}
}
}
}
//定义CreateControlStyle方法
protected virtual Style CreateControlStyle(){ return new Style(ViewState);}
As shown in the code above, ControlStyle is a read-only property whose data type is style. When the property is first accessed, the procedure is: first, determine if the _controlstyle is empty, and if it is empty, call the Createcontrolstyle method to create the _controlstyle object, which is an instance of a style. Then, perform the view-state tracking task, which is done by the TrackViewState method provided by the style class.
After a preliminary understanding of the ControlStyle property, we should then understand the style class that is closely related to the property.
The style class is used to represent the styles of a server control, which includes the following properties:
(1) BackColor, Gets or sets the background color of the Web server control;
(2) BorderColor, Gets or sets the border color of the control;
(3) BorderStyle, Gets or sets the border style of the control;
(4) BorderWidth, Gets or sets the border width of the control;
(5) CssClass, Gets or sets the cascading style sheet class that the control renders on the client;
(6) Font, gets the properties of the fonts associated with the control;
(7) ForeColor, Gets or sets the foreground color of the control;
(8) Height, Gets or sets the altitude of the control;
(9) IsEmpty, gets a value indicating whether any style elements have been defined in the ViewState;
(10) isTrackingViewState, returns a value that indicates whether changes to its view state are being tracked.
(11) Registeredcssclass, gets a cascading style sheet class that has been registered with the control;
(11) ViewState, gets the view state that holds the style element.