![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
代码
protected void SetReadOnly(Control ctrl)
{
if (!ctrl.HasControls())
{
if (ctrl is TextBox)
{
TextBox tb = (TextBox)ctrl;
tb.Enabled = false;
}
if (ctrl is DropDownList)
{
DropDownList ddl = (DropDownList)ctrl;
ddl.Enabled = false;
}
}
else
{
int i = 0;
while (i < ctrl.Controls.Count)
{
SetReadOnly(ctrl.Controls[i]);
i++;
}
}
}
网上很多代码,只能遍历父级控件,但是,一般页面中,会把控件写在DIV,Form,Table等中,这样就无法遍历到了,这个方法可以遍历父级控件中的子控件。