If I create a UserControl and add some objects to it, how can I grab the HTML it would render?
如果我创建一个UserControl并向它添加一些对象,我如何获取它将呈现的HTML ?
ex.
前女友。
UserControl myControl = new UserControl();
myControl.Controls.Add(new TextBox());
// ...something happens
return strHTMLofControl;
I'd like to just convert a newly built UserControl to a string of HTML.
我只想将新构建的UserControl转换为一个HTML字符串。
7 个解决方案
#1
55
You can render the control using Control.RenderControl(HtmlTextWriter)
.
您可以使用control . rendercontrol (HtmlTextWriter)呈现控件。
Feed StringWriter
to the HtmlTextWriter
.
为HtmlTextWriter提供StringWriter。
Feed StringBuilder
to the StringWriter
.
向StringWriter提供StringBuilder。
Your generated string will be inside the StringBuilder
object.
生成的字符串将在StringBuilder对象中。
Here's a code example for this solution:
下面是这个解决方案的代码示例:
StringBuilder myStringBuilder = new StringBuilder();
TextWriter myTextWriter = new StringWriter(myStringBuilder);
HtmlTextWriter myWriter = new HtmlTextWriter(myTextWriter);
myControl.RenderControl(myWriter);
string html = myTextWriter.ToString();
#2
32
//render control to string
StringBuilder b = new StringBuilder();
HtmlTextWriter h = new HtmlTextWriter(new StringWriter(b));
this.LoadControl("~/path_to_control.ascx").RenderControl(h);
string controlAsString = b.ToString();
#3
13
UserControl uc = new UserControl();
MyCustomUserControl mu = (MyCustomUserControl)uc.LoadControl("~/Controls/MyCustomUserControl.ascx");
TextWriter tw = new StringWriter();
HtmlTextWriter hw = new HtmlTextWriter(tw);
mu.RenderControl(hw);
return tw.ToString();
#4
8
Seven years late, but this deserves to be shared.
七年过去了,但这值得分享。
The generally accepted solution - StringBuilder
into StringWriter
into HtmlWriter
into RenderControl
- is good. But there are some gotchas, which I unfortunately ran across while trying to do the same thing. Some controls will throw errors if they're not inside of a Page
, and some will throw errors if they're not inside of a <form>
with runat="server"
. The ScriptManager control exhibits both of these behaviours.
普遍接受的解决方案是:将StringBuilder转换为StringWriter转换为RenderControl。但是有一些问题,我很不幸地在尝试做同样的事情时遇到了。有些控件如果不在页面中,就会抛出错误;有些控件如果不在
I eventually found a workaround here. The gist of it is basically just instantiating a new Page and Form before doing the writer work:
我最终在这里找到了一个变通办法。它的主旨基本上就是在写作者之前实例化一个新的页面和表单:
Page page = new Page();
page.EnableEventValidation = false;
HtmlForm form = new HtmlForm();
form.Name = "form1";
page.Controls.Add(form1);
MyControl mc = new MyControl();
form.Controls.Add(mc);
StringBuilder sb = new StringBuilder();
StringWriter sw = new StringWriter(sb);
HtmlTextWriter writer = new HtmlTextWriter(sw);
page.RenderControl(writer);
return sb.ToString();
Unfortunately, this gives you more markup than you actually need (since it includes the dummy form). And the ScriptManager will still fail for some arcane reason I haven't puzzled out yet. Honestly, it's a whole lot of trouble and not worth doing; the whole point of generating controls in the code-behind is so that you don't have to fiddle around with the markup, after all.
不幸的是,这提供了比实际需要的更多的标记(因为它包含虚拟表单)。而且ScriptManager仍然会失败,因为一些我还没有弄明白的原因。老实说,这是一大堆麻烦,不值得去做;在代码背后生成控件的整个要点是,这样您就不必再去摆弄标记了。
#5
4
override the REnderControl method
覆盖REnderControl方法
protected override void Render(HtmlTextWriter output)
{
output.Write("<br>Message from Control : " + Message);
output.Write("Showing Custom controls created in reverse" +
"order");
// Render Controls.
RenderChildren(output);
}
This will give you access to the writer which the HTML will be written to.
这将使您能够访问将要写入HTML的写入器。
You may also want to look into the adaptive control architecture of asp.net adaptive control architecture of asp.net where you can 'shape' the default html output from controls.
您可能还想了解asp.net自适应控制体系结构的自适应控制体系结构,在这个体系结构中,您可以“塑造”控件的默认html输出。
#6
1
Call it's .RenderControl()
method.
叫它的.RenderControl()方法。
#7
0
You could utilize the HttpServerUtility.Execute
Method, available through HttpContext.Current.Server.Execute
:
您可以使用HttpServerUtility。执行方法,可以通过HttpContext.Current.Server.Execute:
var page = new Page();
var myControl = (MyControl)page.LoadControl("mycontrol.ascx");
myControl.SetSomeProperty = true;
page.Controls.Add(myControl);
var sw = new StringWriter();
HttpContext.Current.Server.Execute(page, sw, preserveForm: false);
The benefit would be that you also trigger the Page_Load event of your user control.
好处是您还可以触发用户控件的Page_Load事件。
MSDN documentation can be found here: https://msdn.microsoft.com/en-us/library/fb04e8f7(v=vs.110).aspx.
MSDN文档可以在这里找到:https://msdn.microsoft.com/en-us/library/fb04e8f7(v=vs.110).aspx。
#1
55
You can render the control using Control.RenderControl(HtmlTextWriter)
.
您可以使用control . rendercontrol (HtmlTextWriter)呈现控件。
Feed StringWriter
to the HtmlTextWriter
.
为HtmlTextWriter提供StringWriter。
Feed StringBuilder
to the StringWriter
.
向StringWriter提供StringBuilder。
Your generated string will be inside the StringBuilder
object.
生成的字符串将在StringBuilder对象中。
Here's a code example for this solution:
下面是这个解决方案的代码示例:
StringBuilder myStringBuilder = new StringBuilder();
TextWriter myTextWriter = new StringWriter(myStringBuilder);
HtmlTextWriter myWriter = new HtmlTextWriter(myTextWriter);
myControl.RenderControl(myWriter);
string html = myTextWriter.ToString();
#2
32
//render control to string
StringBuilder b = new StringBuilder();
HtmlTextWriter h = new HtmlTextWriter(new StringWriter(b));
this.LoadControl("~/path_to_control.ascx").RenderControl(h);
string controlAsString = b.ToString();
#3
13
UserControl uc = new UserControl();
MyCustomUserControl mu = (MyCustomUserControl)uc.LoadControl("~/Controls/MyCustomUserControl.ascx");
TextWriter tw = new StringWriter();
HtmlTextWriter hw = new HtmlTextWriter(tw);
mu.RenderControl(hw);
return tw.ToString();
#4
8
Seven years late, but this deserves to be shared.
七年过去了,但这值得分享。
The generally accepted solution - StringBuilder
into StringWriter
into HtmlWriter
into RenderControl
- is good. But there are some gotchas, which I unfortunately ran across while trying to do the same thing. Some controls will throw errors if they're not inside of a Page
, and some will throw errors if they're not inside of a <form>
with runat="server"
. The ScriptManager control exhibits both of these behaviours.
普遍接受的解决方案是:将StringBuilder转换为StringWriter转换为RenderControl。但是有一些问题,我很不幸地在尝试做同样的事情时遇到了。有些控件如果不在页面中,就会抛出错误;有些控件如果不在
I eventually found a workaround here. The gist of it is basically just instantiating a new Page and Form before doing the writer work:
我最终在这里找到了一个变通办法。它的主旨基本上就是在写作者之前实例化一个新的页面和表单:
Page page = new Page();
page.EnableEventValidation = false;
HtmlForm form = new HtmlForm();
form.Name = "form1";
page.Controls.Add(form1);
MyControl mc = new MyControl();
form.Controls.Add(mc);
StringBuilder sb = new StringBuilder();
StringWriter sw = new StringWriter(sb);
HtmlTextWriter writer = new HtmlTextWriter(sw);
page.RenderControl(writer);
return sb.ToString();
Unfortunately, this gives you more markup than you actually need (since it includes the dummy form). And the ScriptManager will still fail for some arcane reason I haven't puzzled out yet. Honestly, it's a whole lot of trouble and not worth doing; the whole point of generating controls in the code-behind is so that you don't have to fiddle around with the markup, after all.
不幸的是,这提供了比实际需要的更多的标记(因为它包含虚拟表单)。而且ScriptManager仍然会失败,因为一些我还没有弄明白的原因。老实说,这是一大堆麻烦,不值得去做;在代码背后生成控件的整个要点是,这样您就不必再去摆弄标记了。
#5
4
override the REnderControl method
覆盖REnderControl方法
protected override void Render(HtmlTextWriter output)
{
output.Write("<br>Message from Control : " + Message);
output.Write("Showing Custom controls created in reverse" +
"order");
// Render Controls.
RenderChildren(output);
}
This will give you access to the writer which the HTML will be written to.
这将使您能够访问将要写入HTML的写入器。
You may also want to look into the adaptive control architecture of asp.net adaptive control architecture of asp.net where you can 'shape' the default html output from controls.
您可能还想了解asp.net自适应控制体系结构的自适应控制体系结构,在这个体系结构中,您可以“塑造”控件的默认html输出。
#6
1
Call it's .RenderControl()
method.
叫它的.RenderControl()方法。
#7
0
You could utilize the HttpServerUtility.Execute
Method, available through HttpContext.Current.Server.Execute
:
您可以使用HttpServerUtility。执行方法,可以通过HttpContext.Current.Server.Execute:
var page = new Page();
var myControl = (MyControl)page.LoadControl("mycontrol.ascx");
myControl.SetSomeProperty = true;
page.Controls.Add(myControl);
var sw = new StringWriter();
HttpContext.Current.Server.Execute(page, sw, preserveForm: false);
The benefit would be that you also trigger the Page_Load event of your user control.
好处是您还可以触发用户控件的Page_Load事件。
MSDN documentation can be found here: https://msdn.microsoft.com/en-us/library/fb04e8f7(v=vs.110).aspx.
MSDN文档可以在这里找到:https://msdn.microsoft.com/en-us/library/fb04e8f7(v=vs.110).aspx。