Render a user control to a string?
-
I have an ASCX user control that I want to render to a string instead of direct to the browser. I've tried something like: MyControls.footer ftGen = new MyControls.footer(); System.Text.StringBuilder sb = new StringBuilder(); System.IO.StringWriter twriter = new System.IO.StringWriter(sb); HtmlTextWriter writer = new HtmlTextWriter(twriter); ftGen.RenderControl(writer); return sb.ToString(); but am not having any luck. Anybody have ideas? :confused:
-
I have an ASCX user control that I want to render to a string instead of direct to the browser. I've tried something like: MyControls.footer ftGen = new MyControls.footer(); System.Text.StringBuilder sb = new StringBuilder(); System.IO.StringWriter twriter = new System.IO.StringWriter(sb); HtmlTextWriter writer = new HtmlTextWriter(twriter); ftGen.RenderControl(writer); return sb.ToString(); but am not having any luck. Anybody have ideas? :confused:
Hi Chris. I tried this sort of thing once and if I remember right, the error was that the user control wasn't being instanciated in a
<form runat='server'>
server form. Is that what's happening for you? What benefit are you looking for in returning the rendered control as a string? In my case I was attempting to ultimately have the .ascx rendered in the web page after inspecting the string. I ended up just adding the usercontrol to the custom control'sControls
collection and letting normal ASP.NET rendering do its thing. Sorry I can't be more helpful here.