如何以编程方式构建具有窗体和UserControl的System.Web.UI.Page? [英] How can I programmatically build a System.Web.UI.Page with a Form and a UserControl?

查看:83
本文介绍了如何以编程方式构建具有窗体和UserControl的System.Web.UI.Page?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有此代码:

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服务.

This is to test rendering ASCX files and spitting them out of a SOAP/REST service.

问题是,如果某些控件(runat = server的控件)未封装在带有runat = server的标记中,则会失败.

Problem is, some controls (runat=server ones) fail if they are not encapsulated in a tag with runat=server.

解决方案是

The solution to that is here, but the solution assumes being inside an ASPX file where I can just edit the markup.

我将如何以编程方式构建页面,添加表单,设置runat = server以便遵循该解决方案并将控件添加到表单控件中?

How would I programmatically build a Page, add a Form, set runat=server so that I can follow that solution and add my control to the Form Control?

推荐答案

您尝试过这种方法吗?

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

希望这会有所帮助

这篇关于如何以编程方式构建具有窗体和UserControl的System.Web.UI.Page?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆