关闭

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

标签: asp.net测试radiobuttontextboxinputlist
1112人阅读 评论(0) 收藏 举报
分类:

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

   }

0
0

查看评论
* 以上用户言论只代表其个人观点,不代表CSDN网站的观点或立场
    个人资料
    • 访问:62377次
    • 积分:671
    • 等级:
    • 排名:千里之外
    • 原创:4篇
    • 转载:24篇
    • 译文:0篇
    • 评论:3条
    文章分类
    最新评论