4

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.

6
  • What do you mean by "get HTML"? Do you need to access it on the server side, or do you want to show it to your users? How are you going to use the information, and what for? Feb 10, 2009 at 9:34
  • get HTMl-by this I mean I want to get the rendered HTML of the page on click of a button
    – SandHurst
    Feb 10, 2009 at 9:49
  • I still don't get it... Do you want to have the "source code" where? what should happened when you click the button? do you want to treat the code in the server -side or add it directly to the clipboard of the user? what is your accomplishment with this, so we can all understand
    – balexandre
    Feb 10, 2009 at 11:12
  • All I want is to get the rendered HTML.. that will lokk something like--<html><head><title>Hello</title><body>Some Content</body></html>
    – SandHurst
    Feb 10, 2009 at 11:53
  • Do you want to be able to use it on the client side or the server side and how would you like it presented?
    – Sam Becker
    Feb 10, 2009 at 12:21

3 Answers 3

20

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
    base.Render(htw);

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

    // render the markup into the output stream verbatim
    writer.Write(pageSource);

    // 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
}
2
  • 2
    maybe he/she didn't like you :)
    – balexandre
    Feb 10, 2009 at 11:09
  • Why this method doesn't get called when the page loads? Oct 4, 2016 at 9:43
2

Not sure why you want what you want, but... this is off the top of my head, i.e. I didn't try this code.

Add a client-side onclick to your button to show markup and do something like this:

function showMarkup() {
      var markup = "<html>" + document.getElementsByTagName("html")[0].innerHTML + "</html>";

      alert(markup); // You might want to show a div or some other element instead with the markup variable as the inner text because the alert might get cut off.
}

If you need this rendered markup posted back to the server for some reason, store the encoded markup in a hidden input and post that back. You can register the script below on the server-side using ClientScriptManager.RegisterOnSubmitStatement . Here's the cleint-side code.

var markup = escape("<html>" + document.getElementsByTagName("html")[0].innerHTML + "</html>");
var hiddenInput = $get('hiddenInputClientId');

if (hiddenInput) {
      hiddenInput.value = markup;
}

Hope that helps, Nick

-1

I'm still not sure what your objective is with this. But if you want the total rendered output of the page then your probably better of looking at some client side code as this would be run once the server has returned the fully rendered HTML.

Otherwise you could proably catch the page unload event and do something with the rendered content there.

More info needed on what you want from this.

2
  • Not sure why this was down voted, after clarification I woudl agree with Luke (upvoted his answer)
    – Charlie
    Feb 10, 2009 at 15:39
  • 1
    One possible case (three years later): Looking to render a .Net dynamic page into a PDF based on user input / actions on the site.
    – al3xnull
    May 3, 2012 at 13:28

Your Answer

Reminder: Answers generated by Artificial Intelligence tools are not allowed on Stack Overflow. Learn more

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.