Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

Analysis on the use of Attributes and Attributes.CssStyle on ASP.NET pages

2025-02-25 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/02 Report--

This article shows you the analysis of the use of Attributes and Attributes.CssStyle on the ASP.NET page, which is concise and easy to understand, which will definitely brighten your eyes. I hope you can get something through the detailed introduction of this article.

It is well known that when writing WebCustomControl, Attributes and its Attributes.CssStyle attribute, which inherits from the WebControl base class, are very common and important. But it is these two important attributes that can lead to inexplicable efficiency problems if they are not used properly in development.

Because of the flexibility and incompleteness of html, the WebControl base class does not fully represent all the tag attributes and CSS attributes provided and supported by html elements (of course, it is impossible to provide complete attributes due to the compatibility of different browser). And because many html tag attributes and CSS attributes are very obscure and are rarely or rarely used, it will become a burden on WebControl if it is to be fully supported. So the two attributes Attributes and Attributes.CssStyle solve this problem very well, of course, these two attributes not only support the proper html tag attribute and CSS attribute, but also support any legal custom key/value pair. The issue to be discussed here comes from the support for custom key/value pairs.

The type of the Attributes attribute is an AttributeCollection, which is a natural thing, but somehow the constructor of AttributeCollection requires a StateBag parameter:

PublicAttributeCollection (StateBagbag) {this._bag=bag;}

As a result, the ASP.NET pages Attributes and Attributes.CssStyle may be saved in ViewState, but in fact ASP.NET does save its contents to ViewState by default.

This kind of design is really baffling. In the discussion on the efficiency of ViewState, we think that ViewState is really a chicken rib, which is used to maintain some server status and data to make people feel convenient. But it is really crazy to store all the content related to UI in ViewState.

The following is the case of ViewState after using Attributes to define some custom content:

After the ASP.NET pages Attributes and Attributes.CssStyle are automatically saved to ViewState, in addition to the rapid increase in ViewState volume, the burden of Load ViewState in PostBack also increases. The LoadState cost of the page PostBack in the above example is shown below:

In fact, when I wrote the control, it never occurred to me to maintain Attributes and Attributes.CssStyle, or to use the data in it again. And this behavior saved to ViewState by default cannot be customized (at least I haven't found it yet). Later, it occurred to me that during the lifetime of ASP.NET pages, SaveState ends in PreRender, so those who use Attributes and Attributes.CssStyle in Render events will not be saved in ViewState.

Modify the code:

ProtectedoverridevoidOnPreRender (EventArgse) {this.Attributes ["abc"] = "123"; this.Attributes.CssStyle [" abc-style "] =" 123-style "; base.OnPreRender (e);}

In the following form:

ProtectedoverridevoidRender (HtmlTextWriteroutput) {this.Attributes ["abc"] = "123"; this.Attributes.CssStyle [" abc-style "] =" 123-style "; output.Write (Text);}

The ASP.NET pages 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 as follows:

The cost of LoadState is also greatly reduced, with the following overhead:

The above content is the usage analysis of Attributes and Attributes.CssStyle on the ASP.NET page. Have you learned any knowledge or skills? If you want to learn more skills or enrich your knowledge reserve, you are welcome to follow the industry information channel.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report