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

ASP.NET 遍历设置控件的只读属性(测试过)

2011-01-21 16:53 453 查看
for (int i = 0; i < Page.Controls.Count; i++)
{

//重点注意:System.Web.UI.Control ctr in Page.Controls[i].Controls,而不是Page.Controls
foreach (System.Web.UI.Control ctr in Page.Controls[i].Controls)
{
if (ctr is TextBox)
{
TextBox txtControl = (TextBox)ctr;
txtControl.ReadOnly = true;
txtControl.Enabled = false;

}

else if (ctr is RadioButton)
{
RadioButton btn = (RadioButton)ctr;
btn.Enabled = false;
}
else if (ctr is RadioButtonList)
{
RadioButtonList btn = (RadioButtonList)ctr;
btn.Enabled = false;
}
else if (ctr is CheckBox)
{
CheckBox cb = (CheckBox)ctr;
cb.Enabled = false;
}
else if (ctr is DropDownList)
{
DropDownList list = (DropDownList)ctr;
list.Enabled = false;
}
else if (ctr is HtmlTextArea)
{
HtmlTextArea cb = (HtmlTextArea)ctr;
cb.Attributes.Add("readonly", "");
cb.Disabled = true;
}
else if (ctr is HtmlSelect)
{
HtmlSelect rb = (HtmlSelect)ctr;
rb.Disabled = true;
}
else if (ctr is HtmlInputCheckBox)
{
HtmlInputCheckBox rb = (HtmlInputCheckBox)ctr;
rb.Disabled = true;
}
else if (ctr is HtmlInputRadioButton)
{
HtmlInputRadioButton rb = (HtmlInputRadioButton)ctr;
rb.Disabled = true;
}
else if (ctr is HtmlInputText)
{
HtmlInputControl input = (HtmlInputControl)ctr;
input.Attributes.Add("readonly", "readonly");
input.Disabled = true;
}
}

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