Use regular regex to remove Web page EnableViewState implementation Ideas and code _ practical skills

Source: Internet
Author: User

When you create a Web page, you have nothing to write, you can run it, and then look at the source program (view source) and you'll see the following paragraph.

Now, perhaps you will recall that there is a property enableviewstate on the page that can be set to false, but it does not seem to have an effect.

In fact, the Web page has viewstate is not bad, but sometimes we do not need it. What do you do?
First, look at the first picture, the 12th line of code, the highlighted part, is the part that we want to move away from. We can use the regular , then the Web page render, you can take it off.

Reference namespace in. aspx.cs or. Aspx.vb:

Copy Code code as follows:

Using System;
Using System.IO;
Using System.Text;
Using System.Text.RegularExpressions;
Using System.Web;
Using System.Web.UI;

Next, rewrite the render () method.
Copy Code code as follows:

protected override void Render (HtmlTextWriter writer)
{
StringBuilder sb = new StringBuilder ();
StringWriter SW = new StringWriter (SB);
HtmlTextWriter hwriter = new HtmlTextWriter (SW);
Base. Render (Hwriter);
String html = sb. ToString ();
HTML = regex.replace (HTML, "<input[^>]*id=\" (__viewstate) \ "[^>]*>", String. Empty, regexoptions.ignorecase);
Writer. Write (HTML);
}

Of course, the above is only for a single page, if you want multiple pages to remove ViewState, you can write in a basepage base class, need to implement the page, and then inherit this basepage.

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.