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

来源:互联网 发布:java list集合合并 编辑:程序博客网 时间:2024/05/07 13:03

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;   
        }   
     }

   }

原创粉丝点击