Kevin
Kevin

Reputation: 1240

Writing Rendered HTML To A File

I have a web app that displays a table the user can edit. In the process of editing, various inner HTML of the original table gets replaced with edits made by the user. Once the user is finished I would like to save the resulting table in a .html file so that I can include it in a PowerPoint slide without having to recreate it. Basically I want to capture the .aspx file as is after editing and write this to a file.

Any advice is appreciated.

Regards.

Upvotes: 1

Views: 6891

Answers (3)

Tim M.
Tim M.

Reputation: 54359

Basically I want to capture the .aspx file as is

Because you state "capture the ASPX", I'm assuming the data edits are persisted somewhere (memory/database). This means it is straightforward to override the Render() method of the control/page and redirect (or copy) the output stream to a file.

Example

protected override void Render( HtmlTextWriter writer ) {
    using( HtmlTextWriter htmlwriter = new HtmlTextWriter( new StringWriter() ) ) {

        base.Render( htmlwriter );
        string renderedContent = htmlwriter.InnerWriter.ToString();

        // do something here with the string, like save to disk
        File.WriteAllText( @"c:\temp\foo.html", renderedContent );

        // you could also Response.BinaryWrite() the data to the client 
        // so that it could be saved on the user's machine.

        // and/or write it out to the output stream as well
        writer.Write( renderedContent );
    }
}

Sequence

  • User enters data
  • You store the results somewhere
  • You re-render the control with the most recent content
  • Capture the output of the control and write it out to a file

Upvotes: 4

nunespascal
nunespascal

Reputation: 17724

Just a few pointers.

You can get the whole inner html of a section of your page using jquery functions like html

You can use ajax to send this to your server.

You could write an http handler to receive this at the server. Since you only plan to save this to a fixed format html file, nothing else is required. I would not use a page to so as to avoid the extra overhead of creating all the controls, and a page will not receive html input unless you set ValidateRequest to "false".

Upvotes: 1

Biff MaGriff
Biff MaGriff

Reputation: 8231

If you are doing it client side you can do something like this using jquery

http://jsfiddle.net/BAVzC/3/

You can then copy paste to a text file.

Upvotes: 2

Related Questions