如何创建新的DropDownList(或任何asp服务器控件),然后将html呈现为C#中的字符串?
答案 0 :(得分:4)
System.Web.UI.Control有一个RenderControl(HtmlTextWriter)方法,您可以使用该方法将控件的呈现内容作为字符串获取:
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.
}