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

▼魔方 西西 提交于 2019-11-29 07:40:14
Micah

UPDATE This is now a fully working example. There a few things happening here:

  1. Use the call back of a substitution control to render the output of the usercontrol you need.
  2. Use a custom page class that overrides the VerifyRenderingInServerForm and EnableEventValidation to load the control in order to prevent errors from being thrown when the usercontrol contains server controls that require a form tag or event validation.

Here's the markup:

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

Here's the callback

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

Here's the helper class to render the user control

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

See these related questions:

NSjonas

One thing Micah's answer left out is that the substitution function must be static, accept a HttpContext parameter, and return a string. See this msdn page for more info.

I've also extended Micah's helper class to be a little more flexible.

Markup

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

Implemenation

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
}

Class

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 { }
        }
    }

}

Thanks again to Micah for the code

I'm fairly certain you can't do this - the Substitution control will only allow you to insert a string into an outputcached page.
This makes sense if you think about the whole output of a server control, which could be a <table> that'll disrupt all your carefully crafted markup and/or something that requires a load of <script> injected into the page - whereas injecting a single string is something that's relatively straightforward.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!