方法是使用LoadControl方法,根据用户控件的相对路径,动态生成用户控件对象
用户控件
public class UserControlA :UserControl { public UserControlA(string name) { //TODO } }
需要动态生成控件的地方
string ucPath = "../UserControls/UserControlA.ascx"; UserControlA ca = Page.LoadControl(ucPath) as UserControlA;
如果构造函数需要参数的话,则加到路径后面即可。如下
string ucPath = "../UserControls/UserControlA.ascx"; string name = "RI"; UserControlA ca = Page.LoadControl(ucPath,name) as UserControlA;
ASP.NET动态加载用户控件的方法,布布扣,bubuko.com
原文地址:http://www.cnblogs.com/TiestoRay/p/3863612.html