现在的位置: 首页 > 综合 > 正文

.NET动态加载用户控件并传值的方法

2012年01月22日 ⁄ 综合 ⁄ 共 472字 ⁄ 字号 评论关闭

ASPX.CS里的代码:

复制代码 代码如下:
UserControl uc = (UserControl)LoadControl(actionOne[1].ToString());
uc.GetType().GetProperty("cid").SetValue(uc, cid.ToString(), null);//主要就在这里,这里是添加属性,并赋值。
uc.GetType().GetProperty("aid").SetValue(uc, id.ToString(), null);
pageAscx.Controls.Add(uc);

以下是ASCX下面的代码

复制代码 代码如下:
public string _cid = "0";
public string _aid = "0";

public string cid
{
get
{
return _cid;
}
set
{
_cid = value;
}
}
public string aid
{
get
{
return _aid;
}
set
{
_aid = value;
}
}
protected void Page_Load(object sender, EventArgs e)
{
}
}

抱歉!评论已关闭.