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.