I have any ASP.NET control. I want the HTML string how to do I get the HTML string of the control?
4 Answers
This appears to work.
public string RenderControlToHtml(Control ControlToRender)
{
System.Text.StringBuilder sb = new System.Text.StringBuilder();
System.IO.StringWriter stWriter = new System.IO.StringWriter(sb);
System.Web.UI.HtmlTextWriter htmlWriter = new System.Web.UI.HtmlTextWriter(stWriter);
ControlToRender.RenderControl(htmlWriter);
return sb.ToString();
}
6 Comments
Joe Blazek
The StringWriter and HtmlTextWriter both implement IDisposable, so you might want to wrap them in a using() {} block. Also, it might be useful to make this an extension method of 'Control', so you can call it from an instance member.
Princa
@David, how did you end up with render an ascx file to html? I tried above method, but always get "Object reference not set to an instance of an object.", saying the user control I am referencing is not an instance. How should we use it in windows form or console application?
Spongeboy
The default StringWriter() constructor creates a new string builder, so the first two lines can be replaced with just System.IO.StringWriter stWriter = new System.IO.StringWriter();
Tillito
This always gave me an empty string. However, a7drew's solution worked like charme.
Liron Harel
I pass new Controls.sidebar() to the function, but I get empty string, although the control renders html when run inline.
|
Accepted answer by David Basarab will not work if control is not part of the page. a7drew's answer seems unnecessary complex - no need in Context or Server.Execute.
private string RenderControl()
{
var sb = new System.Text.StringBuilder();
using (var stWriter = new System.IO.StringWriter(sb))
using (var htmlWriter = new HtmlTextWriter(stWriter))
{
var p = new Page();
var ctrl = (YourControl)p.LoadControl("~/controls/building blocks/YourControl.ascx");
ctrl.Visible = true;
// do your own init logic if needed
p.Controls.Add(ctrl);
ctrl.RenderControl(htmlWriter);
return sb.ToString();
}
}
3 Comments
Otto G
Thank you, this works! By the way, in my testing, the
ctrl.Visible = true; line seems not to be needed.Adam Mendoza
Thank you! I had a method that worked but for a service but not when called from an API. This works!
liviriniu
I see that
ctrl.Visible = true; as an ensurance / failsafe. In my case, the control had @Visible="false"@ in markup, determining it to be rendered as an empty string.If your control is a web user control, this is how you can get to the HTML it emits from another page or handler:
public void GetHtmlFromMySweetControl(HttpContext context)
{
HttpRequest httpRequest = context.Request;
HttpResponse httpResponse = context.Response;
string foo = httpRequest["foo"];
Page pageHolder = new Page();
string path = "~/usercontrols/MySweetControl.ascx";
MySweetControl ctrl = (MySweetControl)pageHolder.LoadControl(path);
ctrl.BindProducts(foo);
pageHolder.Controls.Add(ctrl);
StringWriter sw = new StringWriter();
context.Server.Execute(pageHolder, sw, false);
httpResponse.Write(sw.ToString());
}
2 Comments
Jason Parker
When I try this, the HTML rendered out is different than what would normally appear on the page. For instance, an asp:Panel with an GroupingText attribute. The value of the GroupingText attribute is rendered now as a fieldset and legend. This is not desirable... its like the UserControl is being rendered under some different kind of mode.... completely different HTML... :(
Tillito
Nice! I did not even know this was possible! This allows very efficient ways of caching content! Thanks a lot!
More compact code:
Control c = (Control)Page.LoadControl("~/Controls/ProjectsCarousel.ascx");
// next, if you want to set property NAME=VALUE
// PropertyInfo propertyInfo = c.GetType().GetProperty("NAME");
// propertyInfo.SetValue(c, Convert.ChangeType("VALUE", propertyInfo.PropertyType), null);
StringBuilder sb = new StringBuilder();
c.RenderControl(new HtmlTextWriter(new StringWriter(sb)));
string result = sb.ToString();