先声明下,这个类是老外写的,非原创,但挺有用的。分享一下
1,枚举
1
using System;
2![](/Images/OutliningIndicators/None.gif)
3
namespace Clingingboy
4![](/Images/OutliningIndicators/ExpandedBlockStart.gif)
![](/Images/OutliningIndicators/ContractedBlock.gif)
{
5![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
/**//// <summary>
6
/// 启用控件枚举
7
/// </summary>
8
public enum ControlName
9![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
10![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
/**//// <summary>
11
/// Includes asp.net button, image button, link button, html button.
12
/// </summary>
13
Button,
14![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
/**//// <summary>
15
/// Includes asp.net text box, html input text, html text area server controls.
16
/// </summary>
17
TextBox,
18![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
/**//// <summary>
19
/// Includes asp.net check box and html input check box.
20
/// </summary>
21
CheckBox,
22![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
/**//// <summary>
23
/// Includes asp.net drop down list, list box, html select.
24
/// </summary>
25
ListControl,
26![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
/**//// <summary>
27
/// Includes asp.net validators.
28
/// </summary>
29
Validator,
30![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
/**//// <summary>
31
/// All the controls that are defined in this enum.
32
/// </summary>
33
All
34
}
35
}
36![](/Images/OutliningIndicators/None.gif)
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![](/Images/OutliningIndicators/None.gif)
11![](/Images/OutliningIndicators/None.gif)
12
namespace Clingingboy
13![](/Images/OutliningIndicators/ExpandedBlockStart.gif)
![](/Images/OutliningIndicators/ContractedBlock.gif)
{
14![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
/**//// <summary>
15
/// 控件启用状态
16
/// </summary>
17
public class ControlEnabler
18![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
19![](/Images/OutliningIndicators/InBlock.gif)
20![](/Images/OutliningIndicators/ContractedSubBlock.gif)
Page Control Enabling / Disabling Utility#region Page Control Enabling / Disabling Utility
21![](/Images/OutliningIndicators/InBlock.gif)
22![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
/**//// <summary>
23
/// 设置是否启用全部控件
24
/// </summary>
25
/// <param name="page"></param>
26
/// <param name="isEnable"></param>
27
public static void SetControlsEnabled(Control page, bool isEnable)
28![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
29
30
SetControlsEnabled(page, ControlName.All, isEnable);
31
}
32![](/Images/OutliningIndicators/InBlock.gif)
33![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
/**//// <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![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
41![](/Images/OutliningIndicators/InBlock.gif)
42
foreach (Control ctrl in page.Controls)
43![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
44![](/Images/OutliningIndicators/InBlock.gif)
45
//==================== considering asp.net server controls ==================
46![](/Images/OutliningIndicators/InBlock.gif)
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![](/Images/OutliningIndicators/InBlock.gif)
52
//Check Boxes
53
if (ctrl is CheckBox && (controlName == ControlName.CheckBox || controlName == ControlName.All))
54
((CheckBox)(ctrl)).Enabled = isEnable;
55![](/Images/OutliningIndicators/InBlock.gif)
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![](/Images/OutliningIndicators/InBlock.gif)
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![](/Images/OutliningIndicators/InBlock.gif)
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![](/Images/OutliningIndicators/InBlock.gif)
84![](/Images/OutliningIndicators/InBlock.gif)
85
//================== considering html server controls ==================
86![](/Images/OutliningIndicators/InBlock.gif)
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![](/Images/OutliningIndicators/InBlock.gif)
102
//=================== RECURSION =======================================
103![](/Images/OutliningIndicators/InBlock.gif)
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![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
108
SetControlsEnabled(ctrl, controlName, isEnable);
109
}
110
}
111
}
112![](/Images/OutliningIndicators/InBlock.gif)
113![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
/**//// <summary>
114
/// 设置是否只读
115
/// </summary>
116
/// <param name="page"></param>
117
/// <param name="isReadOnly"></param>
118
public static void SetTextBoxReadOnly(Control page, bool isReadOnly)
119![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
120![](/Images/OutliningIndicators/InBlock.gif)
121
foreach (Control ctrl in page.Controls)
122![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
123![](/Images/OutliningIndicators/InBlock.gif)
124
//considering asp.net server controls
125
if (ctrl is TextBox)
126
((TextBox)(ctrl)).ReadOnly = isReadOnly;
127![](/Images/OutliningIndicators/InBlock.gif)
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![](/Images/OutliningIndicators/InBlock.gif)
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![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
138
SetTextBoxReadOnly(ctrl, isReadOnly);
139
}
140
}
141
}
142![](/Images/OutliningIndicators/InBlock.gif)
143![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
/**//// <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![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
150![](/Images/OutliningIndicators/InBlock.gif)
151
foreach (Control ctrl in page.Controls)
152![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
153
//considering asp.net server controls
154
if (ctrl is TextBox)
155
((TextBox)(ctrl)).BorderWidth = (isToLabelView ? 0 : 1);
156![](/Images/OutliningIndicators/InBlock.gif)
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![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
161
SetTextBoxReadOnly(ctrl, isToLabelView);
162
}
163
}
164
}
165![](/Images/OutliningIndicators/InBlock.gif)
166
#endregion
167
}
168
}