Completely remove ViewState for specific pages

后端 未结 8 1006
终归单人心
终归单人心 2020-12-06 16:14

I have a site that features some pages which do not require any post-back functionality. They simply display static HTML and don\'t even have any associated code. H

相关标签:
8条回答
  • 2020-12-06 17:17

    You could override Render and strip it out with a Regex.

    Sample as requested. (NB: Overhead of doing this would almost certainly be greater than any possible benefit though!)

    [edit: this function was also useful for stripping all hidden input boxes for using the HTML output as a word doc by changing the MIMEType and file extension]

    protected override void Render(HtmlTextWriter output)
    {
        StringWriter stringWriter = new StringWriter();
    
        HtmlTextWriter textWriter = new HtmlTextWriter(stringWriter);
        base.Render(textWriter);
    
        textWriter.Close();
    
        string strOutput = stringWriter.GetStringBuilder().ToString();
    
        strOutput = Regex.Replace(strOutput, "<input[^>]*id=\"__VIEWSTATE\"[^>]*>", "", RegexOptions.Singleline);
    
        output.Write(strOutput);
    }
    
    0 讨论(0)
  • 2020-12-06 17:18

    In the <% @page... directive at the top of the page, add EnableViewState="False". That will prevent the ViewState for that particular page.

    0 讨论(0)
提交回复
热议问题