[英]Is there a way to create and render asp controls to a html string in the code-behind?
您将如何创建新的DropDownList(或任何ASP服务器控件),然后将html呈现为C#中的字符串?
System.Web.UI.Control has a RenderControl(HtmlTextWriter) method that you can use to get the rendered content of the control as a string: 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.
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.