extracting a UserControl's HTML ?

M

matt

hello,

i have a usercontrol in my app that draws a DropDownList and some text.
elsewhere, i have a need to extract the control's HTML as if it had
been rendered in-page.

typically, when i need to do this for ASP.NET controls, i do the
following:

//some control
HyperLink link = new HyperLink();
link.Text = "sometext";
link.NavigateUrl = "http://www.yahoo.com";

//get its HTML
StringBuilder sb = new StringBuilder();
System.IO.StringWriter sw = new System.IO.StringWriter(sb);
HtmlTextWriter htw = new HtmlTextWriter(sw);
link.RenderControl(htw);

string html = sb.ToString();


however, this technique doesnt seem to be working for my user control:

//my control
Controls.MyControl myControl = new Controls.MyControl();

//get its HTML
StringBuilder sb = new StringBuilder();
System.IO.StringWriter sw = new System.IO.StringWriter(sb);
HtmlTextWriter htw = new HtmlTextWriter(sw);
myControl.RenderControl(htw);

string html = sb.ToString();

....the var html is always empty.

am i missing something, or doesnt this work for usercontrols?


thanks!
matt
 
M

matt

ok, i believe ive found one mistake:

//my control
Controls.MyControl myControl = new Controls.MyControl();

i think may need to be:

//my control
Controls.MyControl myControl =
(Controls.MyControl)LoadControl("~/myControl.ascx");


but, that gives me a new error:

Control 'ddlReports' of type 'DropDownList' must be placed inside a
form tag with runat=server.

....ddlReports is a dropdownlist w/i my usercontrol. the control is
evidently barfing because it is being asked to render itself directly,
w/o being inside of a server-side <form> tag.


so, what to do? putting a server-side <form> tag inside the control
seems problematic, because i wont be able to place it on any other
webform, since that would produce *two* <form> tags.

additionally, i do not want a <form> tag when i extract this
usercontrol's HTML. (tho i could probably parse it out).

how does one attain this seemingly simple goal of extracting a
usercontrol's rendered HTML...?


thanks!
matt
 
A

Anthony Merante

I've had the same issue before. My solution was simple because i didnt WANT
the dropdownlist so i had to visible=false those form controls. I agree, you
prolly dont want to add a <form> inside your UC.

I wonder if you could do a

HtmlForm form = new Htmlform();
myControl.Controls.InsertAt(0,form);

Only insert that HtmlForm inside your export function. See if that will do
the trick.

HTH,
T
 
M

matt

Anthony said:
I wonder if you could do a

HtmlForm form = new Htmlform();
myControl.Controls.InsertAt(0,form);

this does not work, either. same thing, even when i find the control
from w/i that form object.

MS - is there no way to do this?? how does one get the HTML of a
*usercontrol* that contains server-side controls??


matt
 

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,985
Messages
2,570,202
Members
46,777
Latest member
JosefinaAc

Latest Threads

Top