views:

53

answers:

1

How would you create a new DropDownList (or any asp server control) and then render the html to a string in C#?

+3  A: 

System.Web.UI.Control has a RenderControl(HtmlTextWriter) method that you can use to get the rendered content of the control as a string:

using(var sw = new System.IO.StringWriter()) // SW is a buffer into which the control is rendered
using(var writer = new HtmlTextWriter(sw))
{
    myControl.RenderControl(writer);
    return sw.ToString(); // This returns the generated HTML.
}
Matt
awesome.Thanks.
Kenneth J