比如 tabpage1 页面 有30个 textbox 我想 通过
foreach(control tb in tabpage)
   {
         if(tb 是 textbox)
            {
                  tb.readonly =true;
             } 
    }
以上是我的 思路 具体 代码如何实现的呢?? 

解决方案 »

  1.   

    TextBox textBox = tb as TextBox;
    if(textBox != null)
    {
        textBox.ReadOnly = true;
    }
      

  2.   

    foreach(control c in tabpage.Controls)
      {
      if(c is textbox)
      {
      }  
      }
      

  3.   

    但是 c 没有 readonly 的属性呀?? c 没有得到 textbox的属性!!
      

  4.   

    foreach(control c in tabpage.Controls)
      {
      if(c is  Textbox)
      {
        ((TextBox)c).ReadOnly = true;
      }  
      }
      

  5.   

    这是我刚写的,已经过验证     //在后台写方法   
     protected void settxt(Control con)
        {
            foreach (Control cp in con.Controls)
            {
                if (cp.GetType().ToString() == "System.Web.UI.WebControls.TextBox")
                {
                    ((TextBox)cp).Text = "aaaaa";      //为所有文本框赋值
                    ((TextBox)cp).ReadOnly = true;   //设置为只读属性
                }
                else
                {
                    if (cp.HasControls())
                    {
                        settxt(cp);
                    }
                }
            }
        }   //在load事件中调用方法
       settxt(this);
      

  6.   

    foreach(control tb in this.controls)
      {
     TextBox textBox = tb as TextBox;
    if(textBox != null)
    {
      textBox.ReadOnly = true;
    }  }