LoadControl a UserControl - *and* pass in Constructor Parameters

The inability of LoadControl to accept Constructor Parameters is a real pain in the donkey. This post tells you how to get around that hella annoying issue.

The awesome thing about ServerControls in ASP.NET is that you can instantiate them via a constructor, and hence offer a programming model similar to windows forms user controls. Unfortunately, you can't apply the same concept to UserControls because they are really mini pages within themselves. So you are limited to calling something like -

LoadControl("WebUserControl.ascx") ;

Wouldn't it be nice if you could instead do ...

LoadControl("WebUserControl.ascx",constructorparameter1, constructorparameter2, constructorparameter3 ...) ;

So say for instance, it would be nice if the following code would work -

Control toAdd = LoadControl("WebUserControl.ascx","Sahil Malik",5) ;
PlaceHolder1.Controls.Add(toAdd) ;

Note: There is a new overload new in .NET 2.0 which takes the signature LoadControl(Type,object[]), which is very similar to the above. But it is frequently problematic in ASP.NET 2.0 to refer to a user control in a strongly typed manner.

Well, here is how you'd make it work.

1. First of all write the user control (Duhh!!). My user control has a label on it called Label1, and it has the following code-behind.

public partial class WebUserControl : System.Web.UI.UserControl
{
    public WebUserControl()
    {
    }

    public WebUserControl(string labelText, int howManyTimes)
    {
        System.Text.StringBuilder sb = new System.Text.StringBuilder() ;
        for (int i = 1; i <= howManyTimes; i++)
        {
            sb.Append(labelText) ;
            sb.Append("<br>") ;
        }
        Label1.Text = sb.ToString() ;
    }
}

As you can see, I am simply appending the same text over and over again. Note that it is important to explicitly create a default public constructor (i.e. one without parameters) or you'd confuse the ASP.NET runtime.

2. Step #2 is to add the following private method to your default.aspx, or add it as a protected method to the base class of all your pages, or a static method that can somehow access your page object. In my case I kept things simple and added a private method to my default.aspx.

private UserControl LoadControl(string UserControlPath, params object[] constructorParameters)
{       
    List<Type> constParamTypes = new List<Type>() ;
    foreach (object constParam in constructorParameters)
    {
        constParamTypes.Add(constParam.GetType()) ;
    }

    UserControl ctl = Page.LoadControl(UserControlPath) as UserControl;

    // Find the relevant constructor
    ConstructorInfo constructor = ctl.GetType().BaseType.GetConstructor(constParamTypes.ToArray()) ;

    //And then call the relevant constructor
    if (constructor == null)
    {
        throw new MemberAccessException("The requested constructor was not found on : " + ctl.GetType().BaseType.ToString()) ;
    }
    else
    {
        constructor.Invoke(ctl,constructorParameters) ;
    }

    // Finally return the fully initialized UC
    return ctl;
}

3. And finally, add the following to the Page_Load (or any other suitable place) of your default.aspx -

protected void Page_Load(object sender, EventArgs e)
{
    Control toAdd = LoadControl("WebUserControl.ascx","Sahil Malik",5) ;
    PlaceHolder1.Controls.Add(toAdd) ;
}

Bingo, build the website and run, and here is what you see -

So there ya go, now you can call a UserControl pretty much like a ServerControl. Ok good, another major headache solved. :-)

Here is the original link,
http://blah.winsmarts.com/2006/05/20/loadcontrol-a-usercontrol--and-pass-in-constructor-parameters.aspx

posted @ 2009-01-26 12:41  海洋——海纳百川,有容乃大.  阅读(340)  评论(0编辑  收藏  举报