How do I get the HTML output of a UserControl in .NET (C#)?

后端 未结 7 2064
终归单人心
终归单人心 2020-11-28 05:33

If I create a UserControl and add some objects to it, how can I grab the HTML it would render?

ex.

UserControl myControl = new UserControl();
myContr         


        
7条回答
  •  温柔的废话
    2020-11-28 06:25

    //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();
    

提交回复
热议问题