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

ASP.NET (C#)动态在CS代码中给页面增加用户控件并给用户控件的属性赋值

2013年11月05日 ⁄ 综合 ⁄ 共 383字 ⁄ 字号 评论关闭

//动态在CS代码中给页面增加用户控件
UserControl userControl1 = (UserControl)this.LoadControl("../UserCtrls/WordList.ascx");
//给动态添加的用户控件的属性赋值(利用反射技术)首先引用using System.Reflection;命名空间
 //获取用户控件的Type
Type userCtrlType = userControl1.GetType();
//设定用户控件要赋值的属性
PropertyInfo propertyInfo = userCtrlType.GetProperty("showHeader");
//赋值
propertyInfo.SetValue(userControl1, false, null);
//向页面添加控件
Page.Controls.Add(userControl1);

【上篇】
【下篇】

抱歉!评论已关闭.