Net4.0-HTML Purification

Source: Internet
Author: User
ArticleDirectory
    • Set controlrenderingcompatibilityversion
    • Use clientidmode
    • More detailed management of viewstate

When using ASP. NET for web development, I always pay attention to whether the final generated HTML is clean, so I will use repeater to replace controls such as the gridview, Which is controllable and generated HTMLCodeIt is also very clean. Clean HTML has many advantages, such as less code loading speed and easy control of page elements. In aspnet4, the HTML purification is greatly improved. The following text will give a brief introduction.

Set controlrenderingcompatibilityversion

Controlrenderingcompatibilityversion needs to be set in the webconfig file. In the pages node, you can set the 3.5 or 4.0 engine to render HTML. The default value is 4.0.

Create an ASPnet project in vs2010, and a menu control will be provided in site. Master of the project to display the navigation. The Code is as follows:

 <  ASP  :  Menu  ID = "Navigationmenu"  Runat  = "Server"  Cssclass  = "Menu"  Enableviewstate  = "False"  Includestyleblock  = "False"  Orientation  = "Horizontal"> <  Items  > <  ASP  :  Menuitem  Navigateurl = "~ /Default. aspx"  Text  = "Home"/> <  ASP  :  Menuitem  Navigateurl  = "~ /About. aspx"  Text  = "About"/> </  Items  > </  ASP  :  Menu  > 

Set the value of controlrenderingcompatibilityversion to 3.5 and 4.0 respectively. Run the project and see the differences between the page Source Code as follows:

4.0

3.5

 

It can be seen that the 4.0 engine is the UL label generated by the menu control, while the 3.5 engine is the generated table, and the Code complexity is significantly greater. Of course, the menu control is not optimized, but there are other aspects such:

1. Some built-in styles are removed, but style code is generated on the page;

2. The template control removes External table rendering;

3. The Wizard controls the control layer using the GPS operator. 

Use clientidmode

Add other server controls on the page that inherits the masterpage page, and the final html id will change, as shown below:

In aspnet4, clientidmode can be used to change this situation. clientidmode is set in the page command on the page as follows:

<%@PageTitle= ""Language= "C #"Masterpagefile= "~ /Site. Master"Autoeventwireup= "True"Codebehind= "Test. aspx. cs"Inherits= "Urlrewriterdemo. test"Clientidmode= "Static"%>

After setting clientidmode to static, refresh the page to viewSource codeThe Control ID is changed to its original face, as shown below:

Clientidmode has four attribute values: autoid, inherit, predictable, and static. The following buttons are used as an example to list the ID values displayed under these four attribute values:

Autoid: ctl00_maincontent_btntest

Inherit: maincontent_btntest

Predictable: maincontent_btntest

Static: btntest

More detailed management of viewstate

Viewstate is a matter of love and hate, and poor control will make the page very huge. In aspnet4, viewstatemode can be used for more detailed management of viewstate. Viewstatemode can be set at the page level or in the control level, with three property values:

Inherit: inherited from the parent control;

Enabled: When the viewstate of the parent control is disabled, you can set the viewstate of the current control to be available;

Disabled: When the viewstate of the parent control is enalbed, you can disable the viewstate of the current control.

In the previous versions of ASPnet, there was an enableviewstate attribute to set viewstate. However, viewstatemode and enableviewstate are quite different:

Enableviewstate

1. If its value is Boolean, it determines whether viewstate is enabled for its control or sub-control;

2. The priority level is greater than viewstatemode. If enableviewstate is set to false and viewstatemode is set to enabled, the viewstate of the control is disabled;

3. When the enableviewstate of the parent control is set to false, the viewstate of its child control is disabled. viewstate cannot be enabled separately in the Child control.

Viewstatemode

1. It takes effect only when the parent Control and Its enableviewstate are set to true;

2. The child control is not affected by the viewstatemode of the parent control;

3. After viewstatemode is set to disabled in the parent control, you can set viewstatemode to enabled in the Child control to enable the viewstate of the child control.

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.