473,320 Members | 1,804 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,320 software developers and data experts.

Saving contents of page to a string variable

Hi all,

I am using a 3rd party program in a VS2005 web project. The tool takes as
input a string containing HTML and converts it to RTF.
I have been creating a page by dynamically loading UserControls and then
sending the page to the browser.

Now I want to write the contents of the the page to a string variable,
convert that variable to the RTF and send that to the browser and I am stuck
on the "simple" part of getting the contents of the rendered page into a
string var. It also occurs to me that maybe I might not be able to get the
rendered page from the LoadComplete sub.

Below is some code that demonstrates how the 3rd party control should work.
Note that the code
"Dim htmlString = Response.Output" indicates my lack of understanding of how
to get the rendered contents of the page into a string variable, which I am
trying to do. I have been looking at the Response object to accomplish this
task but maybe I am looking in the wrong place.

Also, can I likewise persist the contents of a page that I have assembled in
..NET to a file on the server itself, rather than sending it to the browser?

--------------------------
Protected Sub Common_DeckRTFOutput_LoadComplete(ByVal sender As Object,
ByVal e As System.EventArgs) Handles Me.LoadComplete
Dim htmlString = Response.Output
Dim rtfString As String = ""

'create object (instance) of html2rtf converter
Dim h As SautinSoft.HtmlToRtf.Converter = New
SautinSoft.HtmlToRtf.Converter()
If Not h Is Nothing Then
'set converter options
h.OutputTextFormat = SautinSoft.HtmlToRtf.eOutputTextFormat.Rtf
h.HtmlPath = "C:\development\powercard.net"

'convert strings
rtfString = h.ConvertString(htmlString)
Response.Write(rtfString)
End If
End Sub
-----------------------

Thanks to all for any suggestions...

May 31 '07 #1
6 1963
John,

Are you trying to do a "screen scrape"?? In other words, do you want
to grab the HTML as if you were viewing the source?

There is a great article on 4Guys (http://www.4guysfromrolla.com)
called Screen Scrapes in ASP.NET.

Here is a snippet from that article -

<%@ Import Namespace="System.Net" %>
<script language="VB" runat="server">
Sub Page_Load(sender as Object, e as EventArgs)
'STEP 1: Create a WebClient instance
Dim objWebClient as New WebClient()
'STEP 2: Call the DownloadedData method
Const strURL as String = "http://www.aspmessageboard.com/"
Dim aRequestedHTML() as Byte

aRequestedHTML = objWebClient.DownloadData(strURL)

'STEP 3: Convert the Byte array into a String
Dim objUTF8 as New UTF8Encoding()
Dim strRequestedHTML as String
strRequestedHTML = objUTF8.GetString(aRequestedHTML)
'WE'RE DONE! - display the string
lblHTMLOutput.Text = strRequestedHTML
End Sub
</script>
May 31 '07 #2
I see what you are trying to do.

You can use following code.

1. Move all your HTML to one big UserControl that is a Page right now.

Then in your aspx page

MyUsercontrol cn = LoadControl("~/MyUserControl.ascx")'
StringWriter sw = new StringWriter();
HtmlTextWriter hw = new HtmlTextWriter(sw);
cn.RenderControl(hw);
string sHtml = sw.ToString();

It's C# but you should easily convert it to VB.NET

George.
"John Kotuby" <jo***@powerlist.comwrote in message
news:uF**************@TK2MSFTNGP02.phx.gbl...
Hi all,

I am using a 3rd party program in a VS2005 web project. The tool takes as
input a string containing HTML and converts it to RTF.
I have been creating a page by dynamically loading UserControls and then
sending the page to the browser.

Now I want to write the contents of the the page to a string variable,
convert that variable to the RTF and send that to the browser and I am
stuck on the "simple" part of getting the contents of the rendered page
into a string var. It also occurs to me that maybe I might not be able to
get the rendered page from the LoadComplete sub.

Below is some code that demonstrates how the 3rd party control should
work. Note that the code
"Dim htmlString = Response.Output" indicates my lack of understanding of
how to get the rendered contents of the page into a string variable, which
I am trying to do. I have been looking at the Response object to
accomplish this task but maybe I am looking in the wrong place.

Also, can I likewise persist the contents of a page that I have assembled
in .NET to a file on the server itself, rather than sending it to the
browser?

--------------------------
Protected Sub Common_DeckRTFOutput_LoadComplete(ByVal sender As Object,
ByVal e As System.EventArgs) Handles Me.LoadComplete
Dim htmlString = Response.Output
Dim rtfString As String = ""

'create object (instance) of html2rtf converter
Dim h As SautinSoft.HtmlToRtf.Converter = New
SautinSoft.HtmlToRtf.Converter()
If Not h Is Nothing Then
'set converter options
h.OutputTextFormat = SautinSoft.HtmlToRtf.eOutputTextFormat.Rtf
h.HtmlPath = "C:\development\powercard.net"

'convert strings
rtfString = h.ConvertString(htmlString)
Response.Write(rtfString)
End If
End Sub
-----------------------

Thanks to all for any suggestions...

May 31 '07 #3
You can use the WebClient class to accomplish that.
Here's more info:
http://www.superdotnet.com/Article.aspx?ArticleID=39

--
I hope this helps,
Steve C. Orr,
MCSD, MVP, CSM, ASPInsider
http://SteveOrr.net
"John Kotuby" <jo***@powerlist.comwrote in message
news:uF**************@TK2MSFTNGP02.phx.gbl...
Hi all,

I am using a 3rd party program in a VS2005 web project. The tool takes as
input a string containing HTML and converts it to RTF.
I have been creating a page by dynamically loading UserControls and then
sending the page to the browser.

Now I want to write the contents of the the page to a string variable,
convert that variable to the RTF and send that to the browser and I am
stuck on the "simple" part of getting the contents of the rendered page
into a string var. It also occurs to me that maybe I might not be able to
get the rendered page from the LoadComplete sub.

Below is some code that demonstrates how the 3rd party control should
work. Note that the code
"Dim htmlString = Response.Output" indicates my lack of understanding of
how to get the rendered contents of the page into a string variable, which
I am trying to do. I have been looking at the Response object to
accomplish this task but maybe I am looking in the wrong place.

Also, can I likewise persist the contents of a page that I have assembled
in .NET to a file on the server itself, rather than sending it to the
browser?

--------------------------
Protected Sub Common_DeckRTFOutput_LoadComplete(ByVal sender As Object,
ByVal e As System.EventArgs) Handles Me.LoadComplete
Dim htmlString = Response.Output
Dim rtfString As String = ""

'create object (instance) of html2rtf converter
Dim h As SautinSoft.HtmlToRtf.Converter = New
SautinSoft.HtmlToRtf.Converter()
If Not h Is Nothing Then
'set converter options
h.OutputTextFormat = SautinSoft.HtmlToRtf.eOutputTextFormat.Rtf
h.HtmlPath = "C:\development\powercard.net"

'convert strings
rtfString = h.ConvertString(htmlString)
Response.Write(rtfString)
End If
End Sub
-----------------------

Thanks to all for any suggestions...
May 31 '07 #4
Hi Steve,
I have been using your ExportPanel with good results. Thanks very much.

This latest need came about when the boss saw the DOC output from the
ExportPanel (which I thought was very nice) and said "Where are the custom
logos/graphics that I told our customers we could place in DOC and PDF files
we generate?"

Well as far as I can tell, if an image is included in the HTML and exported
as a DOC attachment, there is only a web reference to the location of the
image... which is not actually part of the DOC file.

So I'm trying a product which claims to actually integrate images into the
RTF document structure itself, thus the need for the grabbing the HTML as it
is sent to the browser.

From what I am seeing, instead of directly calling the page that generates
the HTML, I call a "proxy" page containing the WebClient that then calls the
other page (even if it is on the same server in the same web) and collect
the HTML stream which I can then place in a variable or save to a file,
probably with FSO or something similar in .NET.
--------------------
Dim myStream as Stream =
myWebClient.OpenRead("~/htmlout.aspx?deckid=233")
Dim sr as StreamReader = new StreamReader(myStream)
dim htmlString as string = sr.ReadToEnd().ToString
--------------------

Or something like that to capture stream output to a variable?

Looks great if I can get it to work.

Thanks muchly....
"Steve C. Orr [MCSD, MVP, CSM, ASP Insider]" <St***@Orr.netwrote in
message news:27**********************************@microsof t.com...
You can use the WebClient class to accomplish that.
Here's more info:
http://www.superdotnet.com/Article.aspx?ArticleID=39

--
I hope this helps,
Steve C. Orr,
MCSD, MVP, CSM, ASPInsider
http://SteveOrr.net
"John Kotuby" <jo***@powerlist.comwrote in message
news:uF**************@TK2MSFTNGP02.phx.gbl...
>Hi all,

I am using a 3rd party program in a VS2005 web project. The tool takes as
input a string containing HTML and converts it to RTF.
I have been creating a page by dynamically loading UserControls and then
sending the page to the browser.

Now I want to write the contents of the the page to a string variable,
convert that variable to the RTF and send that to the browser and I am
stuck on the "simple" part of getting the contents of the rendered page
into a string var. It also occurs to me that maybe I might not be able to
get the rendered page from the LoadComplete sub.

Below is some code that demonstrates how the 3rd party control should
work. Note that the code
"Dim htmlString = Response.Output" indicates my lack of understanding of
how to get the rendered contents of the page into a string variable,
which I am trying to do. I have been looking at the Response object to
accomplish this task but maybe I am looking in the wrong place.

Also, can I likewise persist the contents of a page that I have assembled
in .NET to a file on the server itself, rather than sending it to the
browser?

--------------------------
Protected Sub Common_DeckRTFOutput_LoadComplete(ByVal sender As Object,
ByVal e As System.EventArgs) Handles Me.LoadComplete
Dim htmlString = Response.Output
Dim rtfString As String = ""

'create object (instance) of html2rtf converter
Dim h As SautinSoft.HtmlToRtf.Converter = New
SautinSoft.HtmlToRtf.Converter()
If Not h Is Nothing Then
'set converter options
h.OutputTextFormat = SautinSoft.HtmlToRtf.eOutputTextFormat.Rtf
h.HtmlPath = "C:\development\powercard.net"

'convert strings
rtfString = h.ConvertString(htmlString)
Response.Write(rtfString)
End If
End Sub
-----------------------

Thanks to all for any suggestions...

May 31 '07 #5
Thanks George,
I might try Steve's suggestion first, but this looks good also. So many ways
to skin the poor cat, and I couldn't find even one.

"George Ter-Saakov" <gt****@cardone.comwrote in message
news:e$**************@TK2MSFTNGP03.phx.gbl...
>I see what you are trying to do.

You can use following code.

1. Move all your HTML to one big UserControl that is a Page right now.

Then in your aspx page

MyUsercontrol cn = LoadControl("~/MyUserControl.ascx")'
StringWriter sw = new StringWriter();
HtmlTextWriter hw = new HtmlTextWriter(sw);
cn.RenderControl(hw);
string sHtml = sw.ToString();

It's C# but you should easily convert it to VB.NET

George.
"John Kotuby" <jo***@powerlist.comwrote in message
news:uF**************@TK2MSFTNGP02.phx.gbl...
>Hi all,

I am using a 3rd party program in a VS2005 web project. The tool takes as
input a string containing HTML and converts it to RTF.
I have been creating a page by dynamically loading UserControls and then
sending the page to the browser.

Now I want to write the contents of the the page to a string variable,
convert that variable to the RTF and send that to the browser and I am
stuck on the "simple" part of getting the contents of the rendered page
into a string var. It also occurs to me that maybe I might not be able to
get the rendered page from the LoadComplete sub.

Below is some code that demonstrates how the 3rd party control should
work. Note that the code
"Dim htmlString = Response.Output" indicates my lack of understanding of
how to get the rendered contents of the page into a string variable,
which I am trying to do. I have been looking at the Response object to
accomplish this task but maybe I am looking in the wrong place.

Also, can I likewise persist the contents of a page that I have assembled
in .NET to a file on the server itself, rather than sending it to the
browser?

--------------------------
Protected Sub Common_DeckRTFOutput_LoadComplete(ByVal sender As Object,
ByVal e As System.EventArgs) Handles Me.LoadComplete
Dim htmlString = Response.Output
Dim rtfString As String = ""

'create object (instance) of html2rtf converter
Dim h As SautinSoft.HtmlToRtf.Converter = New
SautinSoft.HtmlToRtf.Converter()
If Not h Is Nothing Then
'set converter options
h.OutputTextFormat = SautinSoft.HtmlToRtf.eOutputTextFormat.Rtf
h.HtmlPath = "C:\development\powercard.net"

'convert strings
rtfString = h.ConvertString(htmlString)
Response.Write(rtfString)
End If
End Sub
-----------------------

Thanks to all for any suggestions...


May 31 '07 #6
Thanks...

That makes for 2 suggestions to use the WebClient as a "proxy" browser to
collect the contents of another page. Your added info about converting a
Byte array to String may be a very important step.

<gf****@gmail.comwrote in message
news:11**********************@q75g2000hsh.googlegr oups.com...
John,

Are you trying to do a "screen scrape"?? In other words, do you want
to grab the HTML as if you were viewing the source?

There is a great article on 4Guys (http://www.4guysfromrolla.com)
called Screen Scrapes in ASP.NET.

Here is a snippet from that article -

<%@ Import Namespace="System.Net" %>
<script language="VB" runat="server">
Sub Page_Load(sender as Object, e as EventArgs)
'STEP 1: Create a WebClient instance
Dim objWebClient as New WebClient()
'STEP 2: Call the DownloadedData method
Const strURL as String = "http://www.aspmessageboard.com/"
Dim aRequestedHTML() as Byte

aRequestedHTML = objWebClient.DownloadData(strURL)

'STEP 3: Convert the Byte array into a String
Dim objUTF8 as New UTF8Encoding()
Dim strRequestedHTML as String
strRequestedHTML = objUTF8.GetString(aRequestedHTML)
'WE'RE DONE! - display the string
lblHTMLOutput.Text = strRequestedHTML
End Sub
</script>


May 31 '07 #7

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

Similar topics

8
by: Scaramouche | last post by:
i have the contents of an html page stored within a variable. i would like to parse out the value of the TITLE tag, ie..<TITLE>this_value_is_what_i_want</TITLE> String title=null; int...
1
by: Bob Murdoch | last post by:
We have an asp application that creates reports from a database. The managers would like to create the report, view the results, add a comment, and save the report so that he can send a link to...
0
by: mathieu cupryk | last post by:
in the Button1_Click I need to make the 1st column saved as readonly. How can I do this? using System; using System.Collections; using System.ComponentModel; using System.Data; using...
4
by: dale zhang | last post by:
Hi, I am trying to save and read an image from MS Access DB based on the following article: http://www.vbdotnetheaven.com/Code/Sept2003/2175.asp Right now, I saved images without any...
2
by: Jay Walker | last post by:
I created a custom DataGridColumn based on Marcie Robillard's MSDN Article: Creating Custom Columns for the ASP.NET Datagrid...
0
by: Luis Esteban Valencia | last post by:
in the Button1_Click I need to make the 1st column saved as readonly. How can I do this? using System; using System.Collections; using System.ComponentModel; using System.Data; using...
3
by: jad101 | last post by:
I have an asp.net web page that: 1. Shows a message 2. Posts back (using javascript) 3. Reads contents of a file 4. response.write the contents of the file to the browser My issue is this: ...
3
by: RCS | last post by:
I have an app that I have different "sections" that I want to switch back and forth from, all while having the server maintain viewstate for each page. In other words, when I am on Page1.aspx and...
0
Ajm113
by: Ajm113 | last post by:
Contributing To: Saving RichTextBox from Tab Control Ok, I have a question what would I use if I wanted to do something like a paste function in a Rich Text Box using the code on that thread?...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
0
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 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 former...

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.