Get HTML of current page without ViewState ASP.Net

Is there a way in which I can get the HTML of my current page. On the current page, I want to say that I am working on Default.aspx and want to get HTML by providing it with a button.

How to get it.

+4
html rendering
source share
3 answers

EDITED in response to clarification of requirements

You can override the way your page renders to capture the server side HTML source.

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 } 
+20
source share

Not sure why you need what you want, but ... it's not in my head, i.e. I have not tried this code.

Add the onclick client side to your button to show the 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 rendering markup sent to the server for any reason, store the encoded markup in a hidden input and send it back. You can register the script below on the server side using ClientScriptManager.RegisterOnSubmitStatement. Here is the code with cleint-side.

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

Hope this helps, Nick

+2
source share

I'm still not sure what your goal is. But if you want to get the final output of the page, then you are probably better off looking at some client-side code, as this will be done after the server returns the fully rendered HTML.

Otherwise, you can catch the page unload event and do something with the rendered content.

More info on what you want from this.

-one
source share