先声明下,这个类是老外写的,非原创,但挺有用的。分享一下
1,枚举
1
using System;
2
3
namespace Clingingboy
4

{
5
/**//// <summary>
6
/// 启用控件枚举
7
/// </summary>
8
public enum ControlName
9
{
10
/**//// <summary>
11
/// Includes asp.net button, image button, link button, html button.
12
/// </summary>
13
Button,
14
/**//// <summary>
15
/// Includes asp.net text box, html input text, html text area server controls.
16
/// </summary>
17
TextBox,
18
/**//// <summary>
19
/// Includes asp.net check box and html input check box.
20
/// </summary>
21
CheckBox,
22
/**//// <summary>
23
/// Includes asp.net drop down list, list box, html select.
24
/// </summary>
25
ListControl,
26
/**//// <summary>
27
/// Includes asp.net validators.
28
/// </summary>
29
Validator,
30
/**//// <summary>
31
/// All the controls that are defined in this enum.
32
/// </summary>
33
All
34
}
35
}
36
2,最后一个方法无效,因为Control不存在此属性
1
using System;
2
using System.Text;
3
using System.Web;
4
using System.Web.UI;
5
using System.IO;
6
using System.Net;
7
using System.Text.RegularExpressions;
8
using System.Web.UI.WebControls;
9
using System.Web.UI.HtmlControls;
10
11
12
namespace Clingingboy
13

{
14
/**//// <summary>
15
/// 控件启用状态
16
/// </summary>
17
public class ControlEnabler
18
{
19
20
Page Control Enabling / Disabling Utility#region Page Control Enabling / Disabling Utility
21
22
/**//// <summary>
23
/// 设置是否启用全部控件
24
/// </summary>
25
/// <param name="page"></param>
26
/// <param name="isEnable"></param>
27
public static void SetControlsEnabled(Control page, bool isEnable)
28
{
29
30
SetControlsEnabled(page, ControlName.All, isEnable);
31
}
32
33
/**//// <summary>
34
/// 设置是否启用控件
35
/// </summary>
36
/// <param name="page"></param>
37
/// <param name="controlName"></param>
38
/// <param name="isEnable"></param>
39
public static void SetControlsEnabled(Control page, ControlName controlName, bool isEnable)
40
{
41
42
foreach (Control ctrl in page.Controls)
43
{
44
45
//==================== considering asp.net server controls ==================
46
47
//Text Boxes
48
if (ctrl is TextBox && (controlName == ControlName.TextBox || controlName == ControlName.All))
49
((TextBox)(ctrl)).Enabled = isEnable;
50
((TextBox)(ctrl)).Width = 200;
51
52
//Check Boxes
53
if (ctrl is CheckBox && (controlName == ControlName.CheckBox || controlName == ControlName.All))
54
((CheckBox)(ctrl)).Enabled = isEnable;
55
56
//Buttons
57
if (ctrl is Button && (controlName == ControlName.Button || controlName == ControlName.All))
58
((Button)(ctrl)).Enabled = isEnable;
59
if (ctrl is ImageButton && (controlName == ControlName.Button || controlName == ControlName.All))
60
((ImageButton)(ctrl)).Enabled = isEnable;
61
if (ctrl is LinkButton && (controlName == ControlName.Button || controlName == ControlName.All))
62
((LinkButton)(ctrl)).Enabled = isEnable;
63
64
//List Controls
65
if (ctrl is DropDownList && (controlName == ControlName.ListControl || controlName == ControlName.All))
66
((DropDownList)(ctrl)).Enabled = isEnable;
67
if (ctrl is ListBox && (controlName == ControlName.ListControl || controlName == ControlName.All))
68
((ListBox)(ctrl)).Enabled = isEnable;
69
70
//Validators
71
if (ctrl is RegularExpressionValidator && (controlName == ControlName.Validator || controlName == ControlName.All))
72
((RegularExpressionValidator)(ctrl)).Enabled = isEnable;
73
if (ctrl is RequiredFieldValidator && (controlName == ControlName.Validator || controlName == ControlName.All))
74
((RequiredFieldValidator)(ctrl)).Enabled = isEnable;
75
if (ctrl is CompareValidator && (controlName == ControlName.Validator || controlName == ControlName.All))
76
((CompareValidator)(ctrl)).Enabled = isEnable;
77
if (ctrl is RangeValidator && (controlName == ControlName.Validator || controlName == ControlName.All))
78
((RangeValidator)(ctrl)).Enabled = isEnable;
79
if (ctrl is CustomValidator && (controlName == ControlName.Validator || controlName == ControlName.All))
80
((CustomValidator)(ctrl)).Enabled = isEnable;
81
if (ctrl is ValidationSummary && (controlName == ControlName.Validator || controlName == ControlName.All))
82
((ValidationSummary)(ctrl)).Enabled = isEnable;
83
84
85
//================== considering html server controls ==================
86
87
//Text Boxes
88
if (ctrl is HtmlInputText && (controlName == ControlName.TextBox || controlName == ControlName.All))
89
((HtmlInputText)(ctrl)).Disabled = !isEnable;
90
if (ctrl is HtmlTextArea && (controlName == ControlName.TextBox || controlName == ControlName.All))
91
((HtmlTextArea)(ctrl)).Disabled = !isEnable;
92
//Check Boxes
93
if (ctrl is HtmlInputCheckBox && (controlName == ControlName.CheckBox || controlName == ControlName.All))
94
((HtmlInputCheckBox)(ctrl)).Disabled = !isEnable;
95
//Dropdown Lists
96
if (ctrl is HtmlSelect && (controlName == ControlName.ListControl || controlName == ControlName.All))
97
((HtmlSelect)(ctrl)).Disabled = !isEnable;
98
//Buttons
99
if (ctrl is HtmlInputButton && (controlName == ControlName.Button || controlName == ControlName.All))
100
((HtmlInputButton)(ctrl)).Disabled = !isEnable;
101
102
//=================== RECURSION =======================================
103
104
//if the number of child controls is greater than zero then the current function is called recursively.
105
//otherwise the recursion ends.
106
if (ctrl.Controls.Count > 0)
107
{
108
SetControlsEnabled(ctrl, controlName, isEnable);
109
}
110
}
111
}
112
113
/**//// <summary>
114
/// 设置是否只读
115
/// </summary>
116
/// <param name="page"></param>
117
/// <param name="isReadOnly"></param>
118
public static void SetTextBoxReadOnly(Control page, bool isReadOnly)
119
{
120
121
foreach (Control ctrl in page.Controls)
122
{
123
124
//considering asp.net server controls
125
if (ctrl is TextBox)
126
((TextBox)(ctrl)).ReadOnly = isReadOnly;
127
128
//considering html server controls
129
if (ctrl is HtmlInputText)
130
((HtmlInputText)(ctrl)).Disabled = isReadOnly;
131
if (ctrl is HtmlTextArea)
132
((HtmlTextArea)(ctrl)).Disabled = isReadOnly;
133
134
//if the number of child controls is greater than zero then the current function is called recursively.
135
//otherwise the recursion ends.
136
if (ctrl.Controls.Count > 0)
137
{
138
SetTextBoxReadOnly(ctrl, isReadOnly);
139
}
140
}
141
}
142
143
/**//// <summary>
144
/// 设置外观,此方法无效
145
/// </summary>
146
/// <param name="page">The container page for which the textboxes will be considered.</param>
147
/// <param name="isToLabelView">if set to <c>true</c> [is to label view].</param>
148
public static void SetTextBoxToLabelView(WebControl page, bool isToLabelView)
149
{
150
151
foreach (Control ctrl in page.Controls)
152
{
153
//considering asp.net server controls
154
if (ctrl is TextBox)
155
((TextBox)(ctrl)).BorderWidth = (isToLabelView ? 0 : 1);
156
157
//if the number of child controls is greater than zero then the current function is called recursively.
158
//otherwise the recursion ends.
159
if (ctrl.Controls.Count > 0)
160
{
161
SetTextBoxReadOnly(ctrl, isToLabelView);
162
}
163
}
164
}
165
166
#endregion
167
}
168
}
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· AI与.NET技术实操系列:基于图像分类模型对图像进行分类
· go语言实现终端里的倒计时
· 如何编写易于单元测试的代码
· 10年+ .NET Coder 心语,封装的思维:从隐藏、稳定开始理解其本质意义
· .NET Core 中如何实现缓存的预热?
· 分享一个免费、快速、无限量使用的满血 DeepSeek R1 模型,支持深度思考和联网搜索!
· 25岁的心里话
· 基于 Docker 搭建 FRP 内网穿透开源项目(很简单哒)
· ollama系列01:轻松3步本地部署deepseek,普通电脑可用
· 按钮权限的设计及实现