我如何获得用户控件的.NET(C#)HTML输出? [英] How do I get the HTML output of a UserControl in .NET (C#)?
问题描述
如果我创建了一个用户控件,并添加一些对象吧,我怎么能抢的HTML它会使?
If I create a UserControl and add some objects to it, how can I grab the HTML it would render?
恩。
UserControl myControl = new UserControl();
myControl.Controls.Add(new TextBox());
// ...something happens
return strHTMLofControl;
我想只是转换一个新建的用户控件到HTML的字符串。
I'd like to just convert a newly built UserControl to a string of HTML.
回答(下):
使用<一个href="http://stackoverflow.com/questions/288409/how-do-i-get-the-html-output-of-a-usercontrol-in-net-c#288419">azamsharp's方法奏效 - 这里的code例如:
Using azamsharp's method worked - here's the code example:
TextWriter myTextWriter = new StringWriter();
HtmlTextWriter myWriter = new HtmlTextWriter(myTextWriter);
myControl.RenderControl(myWriter);
return myTextWriter.ToString();
您需要使用System.IO(获得的StringWriter类)。
You'll need to be using System.IO (to get the StringWriter class).
推荐答案
您可以渲染使用控制 Control.RenderControl(HtmlTextWriter的)
。
You can render the control using Control.RenderControl(HtmlTextWriter)
.
订阅的StringWriter
到的HtmlTextWriter
。
订阅的StringBuilder
到的StringWriter
。
您生成的字符串将是的StringBuilder
对象中。
Your generated string will be inside the StringBuilder
object.
这篇关于我如何获得用户控件的.NET(C#)HTML输出?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!