2013-05-13 53 views
1

當使用Server.Execute()的MyUserControl含有ASP(未HTML)的控制我收到以下消息問題渲染使用使用Server.Execute()用戶控件

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) 
+0

什麼是你的問題?您是否檢查了異常消息(通過單擊彈出窗口中「操作」下的「查看詳細信息...」)? – Igor 2013-05-13 14:43:46

+0

錯誤執行子請求意味着有什麼問題INSIDE ascx,... – 2013-05-13 14:44:30

+0

當執行ascx文件時,所有內容都是HTML工作正常,但是當添加asp按鈕控件時不工作 – ZSH 2013-05-13 14:54:51

回答

0

關於頭RUNAT =「服務器」,你可以試試這個:

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

希望這將有助於

+0

我收到此錯誤:「頁面上需要ScriptManager才能使用ASP.NET AJAX腳本組件。 – ZSH 2013-05-19 04:29:30

+0

@ZSH同樣如此:動態地添加一個scriptmanager(更新我的答案)但是,如果你的目標是提取控件的發佈html代碼,恐怕它不會在客戶端工作。 – jbl 2013-05-20 07:48:43