使用Server.Execute()呈现UserControl的问题

时间:2013-05-13 14:39:38

标签: jquery asp.net webforms

当包含asp(不是html)控件的Server.Execute()myusercontrol收到以下消息时

enter image description here

用户控件:

<%@ Control Language="C#" AutoEventWireup="true" CodeBehind="MyWebUserControl1.ascx.cs" Inherits="ImageHandlerClient.WebUserControl1" %>

堆栈跟踪:

 at System.Web.UI.Page.HandleError(Exception e)
   at System.Web.UI.Page.ProcessRequestMain(Boolean includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint)
   at System.Web.UI.Page.ProcessRequest(Boolean includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint)
   at System.Web.UI.Page.ProcessRequest()
   at System.Web.UI.Page.ProcessRequestWithNoAssert(HttpContext context)
   at System.Web.UI.Page.ProcessRequest(HttpContext context)
   at System.Web.HttpServerUtility.ExecuteInternal(IHttpHandler handler, TextWriter writer, Boolean preserveForm, Boolean setPreviousPage, VirtualPath path, VirtualPath filePath, String physPath, Exception error, String queryStringOverride)

1 个答案:

答案 0 :(得分:0)

关于Head runat =“server”,你可以试试这个:

public static string RenderView(string controlName)
{
    var pageHolder = new System.Web.UI.Page();
    var headHolder = new System.Web.UI.HtmlControls.HtmlHead();

    pageHolder.Controls.Add(headHolder);
    var formHolder = new System.Web.UI.HtmlControls.HtmlForm();
    pageHolder.Controls.Add(formHolder);

    // dynamically adding a script manager
    var sm = new System.Web.UI.ScriptManager();
    formHolder.Controls.Add(sm);


    var viewControl = pageHolder.LoadControl(controlName);

    formHolder.Controls.Add(viewControl);

    var output = new StringWriter();
    HttpContext.Current.Server.Execute(pageHolder, output, false);

    return output.ToString();
}

希望这会有所帮助