Writing the asp.net to a file

G

Guest

Is there an option to write the viewed page to a file on the server?
I have an asp.net page that produce a report according to a user
information. I want to add a button that when clicked the page will be saved
to a file on the server. Is that possible?
 
L

Laurent Bugnion

Hi,
Is there an option to write the viewed page to a file on the server?
I have an asp.net page that produce a report according to a user
information. I want to add a button that when clicked the page will be saved
to a file on the server. Is that possible?

Yes, it is possible. The idea is to use a self-created HtmlTextWriter
(which is just a specialized TextWriter), and to pass this writer to all
the controls.

Something like that:

// In Page.Render

if ( Request.QueryString != null
&& Request.QueryString[ "static" ] == "1" )
{
this.RenderStatic( this.Request.ContentEncoding );

// Send a basic response
writer.WriteLine( "Static file saved" );
return;
}



// With:

public void RenderStatic( Encoding oEncoding )
{
HtmlTextWriter writer = null;

try
{
writer = new HtmlTextWriter( @"c:\temp\report.html" );

foreach ( Control child in this.Controls )
{
child.RenderControl( writer );
}
}
catch ( Exception ex )
{
throw ex;
}
finally
{
if ( writer != null )
{
writer.Close();
}
}
}

A few notes:

- The process is triggered by a URL like this:
http://www.domain.com/page.aspx?static=1

- If the Render method directly writes HTML as text to the
HtmlTextWriter, then you must also write this HTML as text to your
"static" writer.

- Once the controls have been rendered to a writer, they cannot be
rendered a second time. This is why I send back a basic response to the
client instead of attempting to render the normal page. If you don't
want to display this basic text in the browser, you can use AJAX
(XmlHttpRequest) to send the request to the URL above, and ignore the
answer.

Feel free to ask if something is not clear.

Greetings,
Laurent
 
G

Guest

Its working but I have a minor problem. All the controls in the page lost
their style attributes (background color,font size ext').

Laurent Bugnion said:
Hi,
Is there an option to write the viewed page to a file on the server?
I have an asp.net page that produce a report according to a user
information. I want to add a button that when clicked the page will be saved
to a file on the server. Is that possible?

Yes, it is possible. The idea is to use a self-created HtmlTextWriter
(which is just a specialized TextWriter), and to pass this writer to all
the controls.

Something like that:

// In Page.Render

if ( Request.QueryString != null
&& Request.QueryString[ "static" ] == "1" )
{
this.RenderStatic( this.Request.ContentEncoding );

// Send a basic response
writer.WriteLine( "Static file saved" );
return;
}



// With:

public void RenderStatic( Encoding oEncoding )
{
HtmlTextWriter writer = null;

try
{
writer = new HtmlTextWriter( @"c:\temp\report.html" );

foreach ( Control child in this.Controls )
{
child.RenderControl( writer );
}
}
catch ( Exception ex )
{
throw ex;
}
finally
{
if ( writer != null )
{
writer.Close();
}
}
}

A few notes:

- The process is triggered by a URL like this:
http://www.domain.com/page.aspx?static=1

- If the Render method directly writes HTML as text to the
HtmlTextWriter, then you must also write this HTML as text to your
"static" writer.

- Once the controls have been rendered to a writer, they cannot be
rendered a second time. This is why I send back a basic response to the
client instead of attempting to render the normal page. If you don't
want to display this basic text in the browser, you can use AJAX
(XmlHttpRequest) to send the request to the URL above, and ignore the
answer.

Feel free to ask if something is not clear.

Greetings,
Laurent
--
Laurent Bugnion, GalaSoft
Software engineering: http://www.galasoft-LB.ch
Private/Malaysia: http://mypage.bluewin.ch/lbugnion
Support children in Calcutta: http://www.calcutta-espoir.ch
 
L

Laurent Bugnion

Hi,
Its working but I have a minor problem. All the controls in the page lost
their style attributes (background color,font size ext').

How do you define the styles? in external CSS files? If yes, you must be
careful that the paths are correct, for example that the static HTML
file is saved in the same folder as the ASPX file (if that's possible).

HTH,
Laurent
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,579
Members
45,053
Latest member
BrodieSola

Latest Threads

Top