473,397 Members | 1,985 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,397 software developers and data experts.

Writing the asp.net to a file

Ido
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?
Sep 5 '06 #1
3 1025
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
Sep 5 '06 #2
Ido
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
Sep 5 '06 #3
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
Sep 5 '06 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

48
by: Joseph | last post by:
Hi I'm writing a commercial program which must be reliable. It has to do some basic reading and writing to and from files on the hard disk, and also to a floppy. I have foreseen a potential...
6
by: Sebastian Kemi | last post by:
How should a write a class to a file? Would this example work: object *myobject = 0; tfile.write(reinterpret_cast<char *>(myobject), sizeof(*object)); / sebek
3
by: ishekar | last post by:
Hi, I have an application where i want to write data to a file, the data is being sent from an external source. I know the total size of the data and then i retrieve the data in small segments...
1
by: Daniel | last post by:
System.IO.StreamWriter Close or Flush method to shut down the computer in such a way that just part of the file is written? or an empty file is written? Also if the Close or Flush is to a...
2
by: melanieab | last post by:
Hi, I'm trying to store all of my data into one file (there're about 140 things to keep track of). I have no problem reading a specific string from the array file, but I wasn't sure how to...
4
by: HNguyen | last post by:
Hi, I have a Web application in ASP.NET. My Application allows the users upload files into the server after checking their user names and passwords. For each transaction, the Web program will...
0
by: Yunus's Group | last post by:
Yunus's Group May 23, 3:36 pm show options Newsgroups: microsoft.public.dotnet.languages.vb From: "Yunus's Group" <yunusasm...@gmail.com> - Find messages by this author Date: 23 May 2005...
16
by: Claudio Grondi | last post by:
I have a 250 Gbyte file (occupies the whole hard drive space) and want to change only eight bytes in this file at a given offset of appr. 200 Gbyte (all other data in that file should remain...
6
by: arne.muller | last post by:
Hello, I've come across some problems reading strucutres from binary files. Basically I've some strutures typedef struct { int i; double x; int n; double *mz;
3
by: Barry Flynn | last post by:
Hi I am working with a VB 2005 program which has been converted from VB6. It writes data out to a flat file, with code like the following line WriteLine(riFileNo, "Hist", lsAssetID,...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.