先看下面的一个例子:
script runat = " server " language = " C# " >
![](http://sooloo.cnblogs.com/Images/OutliningIndicators/None.gif)
![](http://sooloo.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
![](http://sooloo.cnblogs.com/Images/OutliningIndicators/ContractedBlock.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/b854634c0904529d4018c4c3336be836.gif)
![](http://sooloo.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](http://sooloo.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](http://sooloo.cnblogs.com/Images/OutliningIndicators/ExpandedBlockEnd.gif)
![](http://sooloo.cnblogs.com/Images/OutliningIndicators/None.gif)
![](http://sooloo.cnblogs.com/Images/OutliningIndicators/None.gif)
![](http://sooloo.cnblogs.com/Images/OutliningIndicators/None.gif)
![](http://sooloo.cnblogs.com/Images/OutliningIndicators/None.gif)
![](http://sooloo.cnblogs.com/Images/OutliningIndicators/None.gif)
![](http://sooloo.cnblogs.com/Images/OutliningIndicators/None.gif)
![](http://sooloo.cnblogs.com/Images/OutliningIndicators/None.gif)
![](http://sooloo.cnblogs.com/Images/OutliningIndicators/None.gif)
![](http://sooloo.cnblogs.com/Images/OutliningIndicators/None.gif)
![](http://sooloo.cnblogs.com/Images/OutliningIndicators/None.gif)
![](http://sooloo.cnblogs.com/Images/OutliningIndicators/None.gif)
![](http://sooloo.cnblogs.com/Images/OutliningIndicators/None.gif)
![](http://sooloo.cnblogs.com/Images/OutliningIndicators/None.gif)
![](http://sooloo.cnblogs.com/Images/OutliningIndicators/None.gif)
![](http://sooloo.cnblogs.com/Images/OutliningIndicators/None.gif)
![](http://sooloo.cnblogs.com/Images/OutliningIndicators/None.gif)
这个例子列出页面上所有的控件,结果如下:
A List of the Controls in the
Controls
CollectionSystem.Web.UI.LiteralControl -
System.Web.UI.WebControls.Label - lblControlList
System.Web.UI.LiteralControl -
System.Web.UI.HtmlControls.HtmlForm -
System.Web.UI.ResourceBasedLiteralControl -
特别要注意的一点:以上代码没有列出ID=“txtName”的TextBox控件!因为这个TextBox控件包含在Form里面,是Form的一个子控件。而我们的代码 foreach(Control c in Controls) 只关心当前页面Controls的控件,至于子控件却未能涉及。(可以把这些控件理解成一个树状的层次关系)
页面Controls
/ | / //foreach(Control c in Controls)
控件1 控件2 控件3 // 只判断控件1、2、3属于页面Controls
/ / //而未涉及到下属子控件
子控件1 子控件2
为了真正做到遍历所有控件集,可以用递归的方法来实现:
<script runat="server" language="C#">
![](http://sooloo.cnblogs.com/Images/OutliningIndicators/None.gif)
![](http://sooloo.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
![](http://sooloo.cnblogs.com/Images/OutliningIndicators/ContractedBlock.gif)
![](http://sooloo.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](http://sooloo.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://sooloo.cnblogs.com/Images/OutliningIndicators/ContractedSubBlock.gif)
![](http://sooloo.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](http://sooloo.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](http://sooloo.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://sooloo.cnblogs.com/Images/OutliningIndicators/ContractedSubBlock.gif)
![](http://sooloo.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](http://sooloo.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](http://sooloo.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](http://sooloo.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
![](http://sooloo.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
![](http://sooloo.cnblogs.com/Images/OutliningIndicators/ExpandedBlockEnd.gif)
![](http://sooloo.cnblogs.com/Images/OutliningIndicators/None.gif)
![](http://sooloo.cnblogs.com/Images/OutliningIndicators/None.gif)
![](http://sooloo.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
![](http://sooloo.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](http://sooloo.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](http://sooloo.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](http://sooloo.cnblogs.com/Images/OutliningIndicators/ExpandedBlockEnd.gif)
![](http://sooloo.cnblogs.com/Images/OutliningIndicators/None.gif)
![](http://sooloo.cnblogs.com/Images/OutliningIndicators/None.gif)
![](http://sooloo.cnblogs.com/Images/OutliningIndicators/None.gif)
![](http://sooloo.cnblogs.com/Images/OutliningIndicators/None.gif)
![](http://sooloo.cnblogs.com/Images/OutliningIndicators/None.gif)
![](http://sooloo.cnblogs.com/Images/OutliningIndicators/None.gif)
![](http://sooloo.cnblogs.com/Images/OutliningIndicators/None.gif)
![](http://sooloo.cnblogs.com/Images/OutliningIndicators/None.gif)
![](http://sooloo.cnblogs.com/Images/OutliningIndicators/None.gif)
![](http://sooloo.cnblogs.com/Images/OutliningIndicators/None.gif)
![](http://sooloo.cnblogs.com/Images/OutliningIndicators/None.gif)
![](http://sooloo.cnblogs.com/Images/OutliningIndicators/None.gif)
![](http://sooloo.cnblogs.com/Images/OutliningIndicators/None.gif)
![](http://sooloo.cnblogs.com/Images/OutliningIndicators/None.gif)
![](http://sooloo.cnblogs.com/Images/OutliningIndicators/None.gif)
![](http://sooloo.cnblogs.com/Images/OutliningIndicators/None.gif)
![](http://sooloo.cnblogs.com/Images/OutliningIndicators/None.gif)
A List of the Controls in the Controls
Collection
- System.Web.UI.LiteralControl
- System.Web.UI.WebControls.Label
- System.Web.UI.LiteralControl
- System.Web.UI.HtmlControls.HtmlForm
- System.Web.UI.LiteralControl
- System.Web.UI.WebControls.TextBox
- System.Web.UI.LiteralControl
- System.Web.UI.ResourceBasedLiteralControl
这下TextBox控件真的露出了庐山真面目。
/// <summary>
/// 控制控件是否可用
/// </summary>
/// <param name="page"></param>
public static void EnableDisableOneControls(System.Web.UI.Control page, bool bFalg)
{
int nPageControls = page.Controls.Count;
for (int i = 0; i < nPageControls; i++)
{
foreach (System.Web.UI.Control control in page.Controls[i].Controls)
{
if (control.HasControls())
{
EnableDisableOneControls(control, bFalg);
}
else
{
if (control is TextBox)
(control as TextBox).Enabled = bFalg;
if (control is Button)
(control as Button).Enabled = bFalg;
if (control is CheckBox)
(control as CheckBox).Checked = bFalg;
if (control is DropDownList)
(control as DropDownList).Enabled = bFalg;
if (control is UserControl)
(control as UserControl).e = bFalg;
if (control is RadioButtonList)
(control as RadioButtonList).SelectedIndex = -1;
if (control is RadioButton)
(control as RadioButton).Checked = bFalg;
if (control is CheckBoxList)
{
foreach (ListItem item in (control as CheckBoxList).Items)
{
item.Selected = bFalg;
}
}
}//if..else
}//foreach
}//for
}