Monday, March 12, 2012

Want to get rendered page output (i.e., the HTML that is getting sent to the browser).

Ok, so that wasn't really what I wanted to do originally. What I*really* want to do is just grab the HTML for a particular section ofthe page. I discovered I could do this using the RenderControlmethod of the placeholder which contains all the controls I want to getHTML for.
Problem is, some of those controls are form elements. Result?
{sound of screeching metal and glass breaking}
Seems that .NET will refuse to render those objects unless they areinside a <form runat="server"> element, and since I'm trying torender them out by themselves, it won't work. Removing the formelements and replacing them with textual equivalents is a possibilitybut seems like an ugly hack. All I want to do is get the renderedHTML for a a chunk of my page! I didn't think it would becomplicated! Perhaps it's not and I just need some guidance here.
At any rate, I'm not sure what the elegant solution is, but what I'vewound up wanting to do is just grab the entire page output, and use aregex search to grab the part of the page output that I want.
So, how do I do this? I'm not sure. Here's what I'm trying:
Protected Overrides Sub Render(ByVal writer As System.Web.UI.HtmlTextWriter)
Dim sw As StringWriter = New StringWriter()
MyBase.Render(New HtmlTextWriter(sw))
Response.Write(sw.ToString())
End Sub
No I've got the page output in sw, my StringWriter. But this isreally ugly because I can't see it until I get to the Render step.
Does anybody have any pointers as to what I should really be doinghere? Remember, all I *really* want is this: to be able to getall the rendered HTML that is going to be generated by all the childcontrols of a PlaceHolder control.
jbeall--
Regarding this...

jbeall wrote:


...At any rate, I'm not sure what the elegant solution is...


...let me be frank by saying "neither do I"; but, I may have an idea or two.
Regarding this...

jbeall wrote:


...all I *really* want is this: to be able to get all the rendered HTML that is going to be generated by all the child controls of a PlaceHolder control...


...I have done something like this. Not exactly the same thing; but, maybe something like it.
In short, I have made a "dirt simple" UserControl and a companion page that allows one to view the code-infront or code-behind of any page on which the UserControl exists.
It is very simple, it is not elegant, it requires a some setup, it requires some preconditions, and it will not scale well.
That said, it does work, more or less.
The control in action can be seen on almost all of the demos on this page...
http://www.WebLogicArts.com/DemoList.aspx

...and the full source code and setup instructions are available for download on that same page.
Basically, the control opens the file to be viewed, converts certain characters so that they can be displayed in a browser, stuffs the resulting text between 2 HTML pre tags, and saves the result to Session. Next, when and enduser wants to view the source the enduser just clicks a link that exists on the user control, the link opens a new window, redirects to a known ASPX page, that known ASPX page grabs the HTML data from Session, and the HTML data is streamed to the new window's browser. Very simple; but, functional.
This may give you some ideas relative to what you are doing.
HTH.

0 comments:

Post a Comment