asp.net generate static post redundancy code, remove ViewState generated code _ Practical skills

Source: Internet
Author: User
Look at their own site compiled after the source files, there are a lot of messy things, look at the feel uncomfortable
Copy Code code as follows:
<input type= "hidden" name= "__viewstate" id= "__viewstate" value= ...

There is also a form source file, a look to know is ASP.net page
Copy Code code as follows:
<form name= "Form1" method= "Post" action= "smartdropdownlist.aspx" id= "Form1" >

Know cloud Habitat community is also asp.net do, just curious to see the blog home source files, found no these things, then asked the next others,

Online also found a point of information;

summed up the idea of their own:
1, if your page does not have any complex data interaction, such as login you can switch to Ajax login, a single press surface can also be removed, if there is a message can also use AJAX way to complete!

2, background management page, data interaction is generally a little complicated, I feel that there is no need to remove what things, anyway, the background to SEO without any impact, I was curious to see the background of the blog Park is also some
No nonsense, just post code, add the following function to your page code
Copy Code code as follows:

protected override void Render (HtmlTextWriter writer)
{
System.IO.StringWriter html = new System.IO.StringWriter ();
System.Web.UI.HtmlTextWriter tw = new System.Web.UI.HtmlTextWriter (HTML);
Base. Render (TW);
String Temp = html. ToString ();
string S1 = string. Empty, s2 = string. Empty, s3 = string. Empty, S4 = string. Empty, S5 = string. Empty, S6 = string. Empty;
int i = 0;
int j = 0;
i = Temp.indexof ("<form");
if (i > 0)
{
j = Temp.indexof (">", i);
S1 = temp.substring (0, I);
S2 = temp.substring (j + 1, temp.length-j-1);
}
i = s2. IndexOf ("<input type=\" hidden\ "name=\" __viewstate\ "id=\" "__viewstate\");
if (i > 0)
{
j = S2. IndexOf (">", i);
S3 = S2. Substring (0, I);
S4 = S2. Substring (j + 1, S2. Length-j-1);
}
i = S4. IndexOf ("<input type=\" hidden\ "name=\" __eventvalidation\ "id=\" "__eventvalidation\");
if (i > 0)
{
j = S4. IndexOf (">", i);
S5 = S4. Substring (0, I);
S6 = S4. Substring (j + 1, S4. Length-j-1);
Temp = S1 + s3 + S5 + S6;
}
Else
{
Temp = S1 + s3 + S4;
}
Temp = Temp.replace ("</form>", "");
Temp = Temp.replace ("\ r \ n", "");
tw. Close ();
Response.Write (Temp);
}

In fact, rewrite the Render method, and then intercept the HTML source file can be solved, more of their own situation to use, feel good to use it!
If you feel simple, or disdain, do not spray, there are problems please make bricks, thank you!

2. Query the foreigner's solution, is to move the code to the tail, but found that some of the Web page error, then found that the page Enableviewatate=false obvious viewstate code reduction.

   Reason Summary:
(1), this is due to net record control view state. asp.net server controls are logged by default.
If you are certain that a control does not need to record control state, you can assign the value false to his enableviewstate to disable.
   Personal Summary:
Turning off the view is not the final solution, it is only reduced, the most thorough way is to make form forms a normal HTML tag.
Omitting the runat= "server" is OK. Programs, while important, are meaningless if the program is not working.
   User insights:
View state, which is not a useless code. You use server controls, why do you read the information as a control object after each commit? All because of the view state.

View state can be disabled, but using Enableviewstate=false is not exhaustive and you will see __viewstate hidden fields on the page. This is because even if you disable him, there will be a server control where it is used, that is, from runat= "Server", if you will be programming from the normal HTML tags, then the page is clean.

Of course, when view state is disabled, system efficiency is elevated because the program does not parse the value in view state and then initializes the control object. This is efficient for the site, but if it's a referral program, it's a bit of a trouble.
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.