How can I programmatically build a System.Web.UI.Page with a Form and a UserControl?

后端 未结 1 1114
迷失自我
迷失自我 2021-01-15 19:46

I have this code:

public static string RenderView(string path)
{
    Page pageHolder = new Page();
    UserControl viewControl = (UserControl)pageHolder.Load         


        
相关标签:
1条回答
  • 2021-01-15 20:24

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

    Hope this will help

    0 讨论(0)
提交回复
热议问题