您的位置:首页 > 编程语言 > ASP

Asp.net web Control Enable 属性设置

2014-04-05 12:47 351 查看
最近手上有一个很简单的一个小项目,需要查看编辑的历史记录,先前设计的时候把数据都save 到DB了,现在时间紧迫 就不在画新的UI,而是采用以前的edit页面 来显示数据,这里就需要把页面上所有的control都设置为disable。而一般的control是没有Enable属性,只有WebControl才有这个属性。所以默认我们会检查当前的control是否是webcontrol,如果是直接设置enable属性,如果不是我们可以通过反射来查找,这里测试了以下,满足我页面的需求,主要代码实现如下:

public static class ExtendClass
{
public static void SetEnable(this Control ctrl, bool value, Func<Control, bool> fun)
{

bool ret = true;
if (fun != null)
{
ret = fun(ctrl);
}
if (ret)
{
if (typeof(WebControl).IsAssignableFrom(ctrl.GetType()))
{
((WebControl)ctrl).Enabled = value;
}
else
{
PropertyInfo property = ctrl.GetType().GetProperty("Enabled");
if (property != null)
{
property.SetValue(ctrl, value, null);
}
}
}

foreach (Control item in ctrl.Controls)
{
SetEnable(item, value, fun);
}

}

}


测试结果如图:

内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: