簡體   English   中英

如何在替代控件內部使用ASP.Net服務器控件?

[英]How to use ASP.Net server controls inside of Substitution control?

雖然我們在Substitution控件中使用的方法應該返回字符串,那么如何在應該呈現在服務器端的服務器控件上的Web窗體中使用甜甜圈緩存呢?
例如Loginview控件?

UPDATE這是一個完全正常的示例。 這里發生了一些事情:

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

這是標記:

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

請參閱以下相關問題:

米卡(Micah )遺漏的一件事是,替換函數必須是static ,必須接受HttpContext參數並返回string 有關更多信息,請參見此msdn頁面

我還擴展了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提供的代碼

相當確定您不能執行此操作-Substitution控件允許您將字符串插入到outputcached頁面中。
如果您考慮服務器控件的整個輸出,則這很有意義,它可能是<table> ,它將破壞您所有精心制作的標記和/或需要在頁面中注入<script>負載的東西-而注入單個字符串是相對簡單的東西。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM