Html – get HTML of current page without ViewState ASP.Net


Is there any way through which I can get HTML of my current page. By current page I mean let's say I am working on Default.aspx and want to get HTML by providing a button on it.

How to get it.

Best Solution

EDITED in response to clarification of the requirements

You can override the page's render method to capture the HTML source on the server-side.

protected override void Render(HtmlTextWriter writer)
    // setup a TextWriter to capture the markup
    TextWriter tw = new StringWriter();
    HtmlTextWriter htw = new HtmlTextWriter(tw);

    // render the markup into our surrogate TextWriter

    // get the captured markup as a string
    string pageSource = tw.ToString();

    // render the markup into the output stream verbatim

    // remove the viewstate field from the captured markup
    string viewStateRemoved = Regex.Replace(pageSource,
        "<input type=\"hidden\" name=\"__VIEWSTATE\" id=\"__VIEWSTATE\" value=\".*?\" />",
        "", RegexOptions.IgnoreCase);

    // the page source, without the viewstate field, is in viewStateRemoved
    // do what you like with it