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


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


« 
» 
快速导航

Copyright © 2016 phpStudy | 豫ICP备2021030365号-3