我有这段代码:
public static string RenderView(string path)
{
Page pageHolder = new Page();
UserControl viewControl = (UserControl)pageHolder.LoadControl(path);
pageHolder.Controls.Add(viewControl);
StringWriter output = new StringWriter();
HttpContext.Current.Server.Execute(pageHolder, output, false);
return output.ToString();
}
哪个来自:
[WebMethod]
public string GetReportsHTML()
{
string output = "";
output = ViewManager.RenderView("ReportsControl.ascx");
return output;
}
这是为了测试渲染ASCX文件并将它们从SOAP / REST服务中吐出来。
问题是,如果某些控件(runat = server one)未使用runat = server封装在标记中,则会失败。
解决方案是here,但解决方案假设在ASPX文件中我可以编辑标记。
如何以编程方式构建页面,添加表单,设置runat = server以便我可以按照该解决方案将控件添加到表单控件中?
答案 0 :(得分:3)
你尝试过这样的事吗?
public static string RenderView(string path)
{
Page pageHolder = new Page();
System.Web.UI.HtmlControls.HtmlForm formHolder = new System.Web.UI.HtmlControls.HtmlForm();
pageHolder.Controls.Add(formHolder );
UserControl viewControl = (UserControl)pageHolder.LoadControl(path);
formHolder.Controls.Add(viewControl);
StringWriter output = new StringWriter();
HttpContext.Current.Server.Execute(pageHolder, output, false);
return output.ToString();
}
希望这会有所帮助