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.

  • 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? – Tomas Aschan Feb 10 '09 at 09:34
  • get HTMl-by this I mean I want to get the rendered HTML of the page on click of a button –  Feb 10 '09 at 09: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 '09 at 11:12
  • All I want is to get the rendered HTML.. that will lokk something like--HelloSome Content –  Feb 10 '09 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 '09 at 12:21
  • I would like to use it on the server side. except the viewstate I want everything to be there –  Feb 10 '09 at 13:12

3 Answers3

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
}
LukeH
  • 263,068
  • 57
  • 365
  • 409
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

nickytonline
  • 6,855
  • 6
  • 42
  • 76
-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.

Charlie
  • 2,096
  • 2
  • 22
  • 34
  • Not sure why this was down voted, after clarification I woudl agree with Luke (upvoted his answer) – Charlie Feb 10 '09 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 03 '12 at 13:28