Render aspx page as a string

To render a page into a string use a StringWriter object.  This can be useful when making AJAX type of queries.  Using an aspx page for AJAX calls makes it easy to reference session state information.

 

        System.IO.StringWriter htmlStringWriter = new System.IO.StringWriter();

        Server.Execute("Page.aspx", htmlStringWriter);

        string htmlOutput = htmlStringWriter.GetStringBuilder().ToString();

Published Saturday, February 14, 2009 1:14 AM by Kiyoshi

Comments

# re: Render aspx page as a string

Tuesday, June 16, 2009 1:48 PM by smithouse

Nice work.  When my user clicks on the submit button, I save the record and then run your code to email the form.  But when it gets to the 2nd line, Server.Execute..., it starts an endless loop of executing the submit code over and over.  Any idea how to get around this?  Thanks

# re: Render aspx page as a string

Wednesday, April 07, 2010 4:05 AM by habib

Thanks for your nice post.

Is any way i can renderr ascx page as a string?

# re: Render aspx page as a string

Monday, August 30, 2010 5:01 PM by b9chris

Your infinite loop is probably caused by executing the page in the codebehind for that page.

For example:

user visits /admin.aspx

In the codebehind you fire off:

Server.Execute("~/admin.aspx");

*dead*

Leave a Comment

(required) 
(required) 
(optional)
(required)