我怎样才能建立程式设计与窗体和用户控件一个System.Web.UI.Page? [英] How can I programtically build a System.Web.UI.Page with a Form and a UserControl?

查看:103
本文介绍了我怎样才能建立程式设计与窗体和用户控件一个System.Web.UI.Page?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有这样的code:

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 =服务器标签的封装一些控件(RUNAT =服务器的)失败。

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

该解决方案是<一个href=\"http://stackoverflow.com/questions/13777442/control-0-of-type-textbox-must-be-placed-inside-a-form-tag-with-runat-server/13777536#comment20569725_13777536\">here,但解决的办法假设作为一个ASPX文件,在那里我可以编辑的标记里面。

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

我将如何编程构建一个页面,添加一个窗体,RUNAT设置=服务器,这样我可以遵循的解决方案,我的控件添加到窗体控件?

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

推荐答案

你有没有尝试过这样的事情?

Have you tried something like this ?

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

希望这将有助于

这篇关于我怎样才能建立程式设计与窗体和用户控件一个System.Web.UI.Page?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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