programmatically extract page's HTML?

M

matt

hello,

is there a way to programmatically loadup one of your .aspx pages, as
if it had been rendered to an actual request?

something like:

//create the page in memory
SomePage somePage = new SomePage();

//prep the writers
StringBuilder sb = new StringBuilder();
StringWriter sw = new StringWriter(sb);
HtmlTextWriter htw = new HtmlTextWriter(sw);

//render the page
somePage.RenderControl(htw);

//snag its html
string bodyHTML = sb.ToString();


....is there a way to do this? the above doesnt really do anything. the
bodyHTML appears always empty.

im aware of the WebRequest technique, whereby i pass in the page's URL
and get its response streamed back. however, that has a drawback in
that Session is lost.

i need a way for one webpage to get another webpage's content, and that
content is based on somethings that must be passed via any state
method. session, cookie, whatever works. except the querystring -- i
believe theres a character-limit on the QS, and i dont want to even
worry about hitting it.



thanks!
matt
 
M

matt

Karl said:
Place the logic in a user control, and follow the code at:
http://openmymind.net/FAQ.aspx?documentId=45

yes, thats the same technique i have above. i can and have done that
for controls, but there are two problems w/ it:

1) in the case i have in mind, i want the entire end-result-HTML of a
..aspx page. the final HTML, after all its controls have rendered, as it
would be if being sent back to an actual web browser. not just one
control's.

2) the user-control technique is, to the best of my knowledge, flawed.
if you try to render a usercontrol that contains one or more child
server-side controls such as a dropdownlist or repeater, then it will
break -- saying that the usercontrol's children must be w/i a <form>
tag. simply, it will not let you programatically render a usercontrol
containing children controls. see this thread:

http://tinyurl.com/ortqr

....so the question remains: is there no way to render a .aspx page into
a string?

btw, regarding the above #2 -- do you have a workable solution for
this? thats been another challenge..



thanks,
matt
 
B

bruce barker \(sqlwork.com\)

you are on the right track, but calling render is not enough, as this will
not fire page load, load viewstate session state, etc. you need to call
ProcessRequest(HttpContext) where you build your own context. care will be
needed with session as its serialized, so you need to supply your own
session manager.

-- bruce (sqlwork.com)
 
R

Ray Booysen

Hi Matt

Here is a method I use

StringWriter _writer = new StringWriter();
Context.Server.Execute("MyPage.aspx" _writer);

You'll then have a StringWriter with the HTML inside.

Hope this helps.

Regards
Ray
 
M

matt

Ray said:
Here is a method I use

StringWriter _writer = new StringWriter();
Context.Server.Execute("MyPage.aspx" _writer);

yep, thats it! like so:

StringWriter writer = new StringWriter();
Server.Execute(""somePage.aspx", writer);

string bodyHTML = writer.ToString();

....and that grabs it. more, it also maintains state between the two!
excellent!


thanks,
matt
 
R

Ray Booysen

Glad this helps.

Just make sure you handle any exceptions that the Execute() method
throws. If I remember correctly if there is an exception thrown in the
page you are executing, there is a generic exception thrown by the
Execute() method that you would need to handle.

Regards
Ray
 

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

No members online now.

Forum statistics

Threads
473,982
Messages
2,570,185
Members
46,736
Latest member
AdolphBig6

Latest Threads

Top