The style Class in the WebControls namespace is a top-level style class. Most standard controls have their style attributes.
1. The following describes how to set the style.
(1) You can directly set the control style
- Button1.BackColor = System.Drawing.Color.Red;
(2) Set it by getting the style set of the web Control
- Button1.ControlStyle.BackColor = System.Drawing.Color.Red;
(3) set the style class and use the ApplyStyle method of the WebControl class to copy non-empty styles and rewrite existing styles.
- myStyle.BackColor = System.Drawing.Color.Red;
- Button1.ApplyStyle(myStyle);
(4) always define the style sheet attributes. Do not use the control attributes. They are the same as HTML styles.
- style="background-color: red"
The following introduces the topic. Why do we need to use styles? We know that you can use a uniform style and a defined style to define a style. You can use it again. Then let's look at the above style setting method.
2. Learn about WebControl. BackColor and Style. BackColor.
(1) is similar to (2. however, (3) is different. (3) The definition method is universal. You can define a style and then use the ApplyStyle method of the control to reference the style. it provides an area for style programming.
The WebControl class defines general styles. (1) and (2) use different style attributes than (3 ).
3. ASP. NET custom style attributes
At the beginning, I talked about the general top-level style class for the style class, but the requirement will change. Well, the encoding is started below.
Here is an example of modifying the label control.
(1) rewrite the style attributes and set the Default background to Red. I believe you can understand it.
- namespaceCustomComponents
- {
- [ToolboxData(@"<{0}:ImageLabel1
- BackColor='Red'
- runat='server'></{0}:ImageLabel1>")
- ]
- publicclassImageLabel1:Label
- {
- publicoverridestringText
- {
- get{returnViewState["Text"]!=null?(string)ViewState["Text"]:base.ID;}
- set{ViewState["Text"]=value;}
- }
-
- publicoverrideSystem.Drawing.ColorBackColor
- {
- get
- {
- returnbase.BackColor=System.Drawing.Color.Red;
- }
- set
- {
- base.BackColor=value;
- }
- }
- }
- }
(2) Add a background image attribute for the label, rewrite the AddAttributesToRender method, and add a style attribute. The AddAttributesToRender method has been mentioned before.
- NamespaceCustomComponents
- {
- PublicclassImageLabel2: Label
- {
- [BrowsableAttribute (true)]
- [DescriptionAttribute ("background")]
- [CategoryAttribute ("Appearance")]
- PublicvirtualStringImageUrl
- {
- Get {returnViewState ["imageUrl"]! = Null? (String) ViewState ["imageUrl"]: "";}
- Set {ViewState ["imageUrl"] = value ;}
- }
- OverrideprotectedvoidAddAttributesToRender (HtmlTextWriterwriter)
- {
- Writer. AddStyleAttribute (HtmlTextWriterStyle. BackgroundImage, ImageUrl );
- Base. AddAttributesToRender (writer );
- }
- }
- }
(3) In the example 2 above, we define the background style. net has done a good job for us. Many style classes are derived from the style class, and the attributes of the style class are extended to meet the needs of different control styles.
(4) use the derived style class to define the control style attributes. as mentioned in example 4, the style attribute of the control is not defined and only the CreateControlStyle method is rewritten. this means that you can directly use the properties in the TableStyle class for the style attributes defined by the control, but the default style attributes are those in the style class, so you need to forcibly convert them.
4. custom typed style attributes
If style attributes cannot meet your needs, you can use custom typed styles.
What is a custom typed style? That is, the class is derived from the style class, and it is modified and expanded (it is written in the book... so I understand -_-)
For example, the Table control defines the style attributes of the ASP. NET Control and the TableStyle class. You can select the style attributes of the control and the TableStyle class.
However, the TableStyle class is universal and flexible. Now let's start to read the code again. Of course, it starts from a simple one.
(1) simple display of style attributes
Note the following:
1. Override the LabelStyle (StateBag viewState) constructor
2. style attributes must be declared by view status.
3. the overloaded AddAttributesToRender method of the Style Class requires two parameter methods.
AddAttributesToRender (HtmlTextWriter writer, WebControl owner)
2) use Programming
The following describes how to add ASP. NET custom style attributes to the control programmatically. If we find that the BackColor attribute can be rendered but the ImageUrl cannot be rendered, it means that the custom class has no meaning, and we have not rewritten a method.
- ProtectedvoidPage_Load (objectsender, EventArgse)
- {
- // Default label Control
- TableStylea=NewTableStyle();
- A. BackImageUrl="Images4.bmp";
- A. BackColor=System. Drawing. Color. Red;
- Label1.ApplyStyle ();
- // Custom control
- ImageLabel3_1.ApplyStyle ();
- }
- Microsoft released multiple function updates for ASP. net mvc 2 preview Edition
- ASP. NET Server custom control security guidelines
- Analysis of ASP. NET programming standards and Their encoding specifications
- Introduction to ASP. NET sessions
- ASP. NET programming tool ASP. NET Web Matrix