Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > ASP .Net > Writing the asp.net to a file

Reply
Thread Tools

Writing the asp.net to a file

 
 
=?Utf-8?B?SWRv?=
Guest
Posts: n/a
 
      09-05-2006
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?
 
Reply With Quote
 
 
 
 
Laurent Bugnion
Guest
Posts: n/a
 
      09-05-2006
Hi,

Ido wrote:
> 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
 
Reply With Quote
 
 
 
 
=?Utf-8?B?SWRv?=
Guest
Posts: n/a
 
      09-05-2006
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" wrote:

> Hi,
>
> Ido wrote:
> > 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
>

 
Reply With Quote
 
Laurent Bugnion
Guest
Posts: n/a
 
      09-05-2006
Hi,

Ido wrote:
> 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
--
Laurent Bugnion, GalaSoft
Software engineering: http://www.galasoft-LB.ch
PhotoAlbum: http://www.galasoft-LB.ch/pictures
Support children in Calcutta: http://www.calcutta-espoir.ch
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Any problems with writing the information into a file - Multi-users perform writing the same file at the same time ???? HNguyen ASP .Net 4 12-21-2004 01:53 PM
Question: Writing text file based TestBenches vs. Waveform file based simulation. BLF VHDL 4 08-07-2004 12:44 AM
File Access error - writing to .txt file John Carnahan ASP .Net 2 07-18-2003 10:35 PM
A failure occurred writing to the resources file. Access is denied. -- RESX file is locked? -- WHY? Mark Kamoski ASP .Net 1 07-04-2003 12:02 PM



Advertisments