2010-06-29 140 views

回答

6

UPDATE 這是一個完整的工作示例。這裏發生了一些事情:

  1. 使用替換控件的回調來呈現您需要的usercontrol的輸出。
  2. 使用覆蓋VerifyRenderingInServerForm和EnableEventValidation的自定義頁面類來加載控件,以防止在usercontrol包含需要表單標記或事件驗證的服務器控件時引發錯誤。

這裏的標記:

<asp:Substitution runat="server" methodname="GetCustomersByCountry" /> 

這裏的回調

public string GetCustomersByCountry(string country) 
{ 
    CustomerCollection customers = DataContext.GetCustomersByCountry(country); 

    if (customers.Count > 0) 
     //RenderView returns the rendered HTML in the context of the callback 
     return ViewManager.RenderView("customers.ascx", customers); 
    else 
     return ViewManager.RenderView("nocustomersfound.ascx"); 
} 

這裏的輔助類來呈現用戶控制

public class ViewManager 
{ 
    private class PageForRenderingUserControl : Page 
    { 
     public override void VerifyRenderingInServerForm(Control control) 
     { /* Do nothing */ } 

     public override bool EnableEventValidation 
     { 
      get { return false; } 
      set { /* Do nothing */} 
     } 
    } 

    public static string RenderView(string path, object data) 
    { 
     PageForRenderingUserControl pageHolder = new PageForUserControlRendering(); 
     UserControl viewControl = (UserControl) pageHolder.LoadControl(path); 

     if (data != null) 
     { 
      Type viewControlType = viewControl.GetType(); 
      FieldInfo field = viewControlType.GetField("Data"); 
      if (field != null) 
      { 
       field.SetValue(viewControl, data); 
      } 
      else 
      { 
       throw new Exception("ViewFile: " + path + "has no data property"); 
      } 
     } 

     pageHolder.Controls.Add(viewControl); 
     StringWriter result = new StringWriter(); 
     HttpContext.Current.Server.Execute(pageHolder, result, false); 
     return result.ToString(); 
    } 
} 

看到這些相關的問題:

+0

太棒了!謝謝 – mohamadreza 2010-07-25 08:05:43

+0

-1參考資料來源:http://weblogs.asp.net/scottgu/archive/2006/10/22/Tip_2F00_Trick_3A00_-Cool-UI-Templating-Technique-to-use-with-ASP.NET-AJAX-for -non_2D00_UpdatePanel-scenarios.aspx – 2012-09-10 16:22:05

-1

相當某些你不能做到這一點 - Substitution控件將允許你插入一個字符串轉換爲outputcached頁。
如果您考慮服務器控件的整個輸出,這可能是一個<table>,這會破壞您的精心製作的標記和/或需要加載<script>注入頁面的東西 - 而注入單個字符串是相對簡單的東西。

0

一個Micah的回答漏掉的是,替代函數必須static,接受HttpContext參數,並返回一個東西string。有關更多信息,請參閱this msdn page

我也擴展了Micah的輔助類,使它更加靈活一些。

標記

<asp:Substitution ID="Substitution1" MethodName="myFunction" runat="server" /> 

再次實行

public static string myFunction(HttpContext httpContext){ 
    ViewManager vm = new ViewManager(); 

    //example using a Button control 

    Button b = new Button(); 
    b.Text = "click me"; //we can set properties like this 

    //we can also set properties with a Dictionary Collection 
    Dictionary<string,object> data = new Dictionary<string,object>(); 
    data.add("Visible",true); 

    String s = vm.RenderView(b,data); //don't do anything (just for example) 

    //we can also use this class for UserControls 
    UserControl myControl = vm.GetUserControl("~mypath"); 

    data.clear(); 
    data.add("myProp","some value"); 

    return vm.RenderView(myControl,data); //return for Substitution control 
} 

using System.IO; 
using System.ComponentModel; 
public class ViewManager 
{ 
    private PageForRenderingUserControl pageHolder; 
    public ViewManager() 
    { 
     pageHolder = new PageForRenderingUserControl(); 
    } 

    public UserControl GetUserControl(string path) 
    { 
     return (UserControl)pageHolder.LoadControl(path); 
    } 

    public string RenderView(Control viewControl, Dictionary<string, object> data) 
    { 
     pageHolder.Controls.Clear(); 
     //Dim viewControl As UserControl = DirectCast(pageHolder.LoadControl(Path), UserControl) 

     if (data != null) { 
      Type viewControlType = viewControl.GetType(); 


      dynamic properties = TypeDescriptor.GetProperties(viewControl); 

      foreach (string x in data.Keys) { 
       if ((properties.Item(x) != null)) { 
        properties.Item(x).SetValue(viewControl, data[x]); 
       } 
      } 
     } 

     pageHolder.Controls.Add(viewControl); 
     StringWriter result = new StringWriter(); 
     HttpContext.Current.Server.Execute(pageHolder, result, false); 
     return result.ToString(); 
    } 

    private class PageForRenderingUserControl : Page 
    { 
     public override void VerifyRenderingInServerForm(Control control) 
     { 
      // Do nothing 
     } 

     public override bool EnableEventValidation { 
      get { return false; } 
      // Do nothing 
      set { } 
     } 
    } 

} 

由於Micah代碼

相關問題