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 -

2009012612395423.gif

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

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值