ASP. NET custom style attributes

Source: Internet
Author: User

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

 
 
  1. Button1.BackColor = System.Drawing.Color.Red; 

(2) Set it by getting the style set of the web Control

 
 
  1. 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.

 
 
  1. myStyle.BackColor = System.Drawing.Color.Red;  
  2. Button1.ApplyStyle(myStyle); 

(4) always define the style sheet attributes. Do not use the control attributes. They are the same as HTML styles.

 
 
  1. 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.

 
 
  1. namespaceCustomComponents  
  2. {  
  3. [ToolboxData(@"<{0}:ImageLabel1  
  4. BackColor='Red' 
  5. runat='server'></{0}:ImageLabel1>")  
  6. ]  
  7. publicclassImageLabel1:Label  
  8. {  
  9. publicoverridestringText  
  10. {  
  11. get{returnViewState["Text"]!=null?(string)ViewState["Text"]:base.ID;}  
  12. set{ViewState["Text"]=value;}  
  13. }  
  14.  
  15. publicoverrideSystem.Drawing.ColorBackColor  
  16. {  
  17. get  
  18. {  
  19. returnbase.BackColor=System.Drawing.Color.Red;  
  20. }  
  21. set  
  22. {  
  23. base.BackColor=value;  
  24. }  
  25. }  
  26. }  

(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.

 
 
  1. NamespaceCustomComponents
  2. {
  3. PublicclassImageLabel2: Label
  4. {
  5. [BrowsableAttribute (true)]
  6. [DescriptionAttribute ("background")]
  7. [CategoryAttribute ("Appearance")]
  8. PublicvirtualStringImageUrl
  9. {
  10. Get {returnViewState ["imageUrl"]! = Null? (String) ViewState ["imageUrl"]: "";}
  11. Set {ViewState ["imageUrl"] = value ;}
  12. }
  13. OverrideprotectedvoidAddAttributesToRender (HtmlTextWriterwriter)
  14. {
  15. Writer. AddStyleAttribute (HtmlTextWriterStyle. BackgroundImage, ImageUrl );
  16. Base. AddAttributesToRender (writer );
  17. }
  18. }
  19. }

(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.

 
 
  1. ProtectedvoidPage_Load (objectsender, EventArgse)
  2. {
  3. // Default label Control
  4. TableStylea=NewTableStyle();
  5. A. BackImageUrl="Images4.bmp";
  6. A. BackColor=System. Drawing. Color. Red;
  7. Label1.ApplyStyle ();
  8. // Custom control
  9. ImageLabel3_1.ApplyStyle ();
  10. }
  1. Microsoft released multiple function updates for ASP. net mvc 2 preview Edition
  2. ASP. NET Server custom control security guidelines
  3. Analysis of ASP. NET programming standards and Their encoding specifications
  4. Introduction to ASP. NET sessions
  5. ASP. NET programming tool ASP. NET Web Matrix

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.