ASP. NET page Attributes and Attributes. CssStyle

Source: Internet
Author: User

As we all know, when compiling WebCustomControl, Attributes inherited from the basic class Attributes of WebControl and Its Attributes. CssStyle Attributes are very common and important. But it is these two important attributes. Improper use during development will lead to inexplicable efficiency problems.

Due to the flexibility and incompleteness of html, The WebControl base class does not fully display all the tag attributes and CSS Attributes provided and supported by html elements. Of course, due to compatibility issues with different browsers, it is impossible to provide complete attributes ). Many html tag attributes and CSS attributes are very uncommon and rarely used. To provide complete support, WebControl becomes a burden. Therefore, Attributes and Attributes. cssStyle solves this problem well. Of course, these two attributes not only support the html tag attributes and CSS attributes, but also support any valid custom key/value pairs. The question to be discussed here is the support for the custom key/value pair.

The type of the Attributes attribute is an AttributeCollection, which is a natural thing, but I don't know how to do it. The AttributeCollection constructor needs a StateBag parameter:

 
 
  1. publicAttributeCollection(StateBagbag)  
  2. {  
  3. this._bag=bag;  

The result is that Attributes and Attributes. CssStyle on the ASP. NET page may be stored in ViewState. In fact, ASP. NET does save the content to ViewState by default.

This kind of design is really confusing. During discussions on ViewState efficiency, we thought ViewState was indeed a weakness, so it was easy to maintain some server states and data. However, it is crazy to store all the content related to the UI into the ViewState.

The following describes how to use Attributes to define ViewState after custom content:


After Attributes and Attributes. CssStyle of ASP. NET pages are automatically saved to ViewState, the Load of ViewState increases at the same time after the ViewState volume increases rapidly. The LoadState cost of page PostBack in the above example, for example:


In fact, when writing controls, I never thought about maintaining Attributes and Attributes. CssStyle, or trying to use the data again. In addition, this default save to ViewState behavior can not be customized, at least I have not found), and later thought in ASP. NET page survival period, SaveState ends in PreRender, so Attributes and Attributes are used in the Render event. cssStyle will not be saved to ViewState.

Modify the Code:

 
 
  1. protectedoverridevoidOnPreRender(EventArgse)  
  2. {  
  3. this.Attributes["abc"]="123";  
  4. this.Attributes.CssStyle["abc-style"]="123-style";  
  5. base.OnPreRender(e);  

The format is as follows:

 
 
  1. protectedoverridevoidRender(HtmlTextWriteroutput)  
  2. {  
  3. this.Attributes["abc"]="123";  
  4. this.Attributes.CssStyle["abc-style"]="123-style";  
  5. output.Write(Text);  

The ASP. NET page Attributes and Attributes. CssStyle will no longer be saved to ViewState. After the above AnalysisReport is modified according to the above example, the running effect of binding the same data is:

The LoadState cost is also greatly reduced, and its overhead is:


  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.