自己写的代码如下:
public void disableControls(System.Web.UI.WebControls.Panel panel)
{
string type="";
         string mButtonTypeString="System.Web.UI.WebControls.Button";
System.Web.UI.WebControls.Button bt = null; foreach (System.Web.UI.Control  cc  in panel.Controls)
{
    type = cc.GetType().ToString();
            if (type.CompareTo(mButtonTypeString)==0)
   {
      bt =(System.Web.UI.WebControls.Button) cc;
      bt.Enabled = false;
   }
}
}
debug时发现,cc.GetType()返回的都是LiteralControl. 何解? 
当遇到Button时,为什么不是Button?

解决方案 »

  1.   

    "panel内"的意思应该是该panel是内部那些控件的容器控件
    这样panel.Enabled = false;就可以了,也不需要这样循环
      

  2.   

    关于类型问题,我以前大概遇到这样的情况,好像要把Control强行转换成你所要对应的类型才行
    至于整个Panel,就如速马兄所说的,Enable=flase就好了!
      

  3.   

    panel里的意思是放在panel里的控件,如下:
    <asp:panel id="panel1" runat="server">
    <asp:TextBox id="textBox1" runat="server" Width="100px"></asp:TextBox>
    </asp:Panel>使用panel.Enabled=false的结果是,整个panel变灰色了,但是里面的控件还是照样可以操作,如在文本框输入,点button还是有反应.
      

  4.   

    FindControl()需要control的ID, 我就是不想用ID一个一个的disable. 我想做一个通用的方法.
      

  5.   

    public void disableControls(System.Web.UI.WebControls.Panel panel)
    {
    string type="";
             string mButtonTypeString="System.Web.UI.WebControls.Button";
    System.Web.UI.WebControls.Button bt = null; foreach (System.Web.UI.Control  cc  in panel.Controls) //这里声明cc为Control
    {
        type = cc.GetType().ToString();             //所以这里type=Control
                if (type.CompareTo(mButtonTypeString)==0)    //所以这里条件判断为假
       {
          bt =(System.Web.UI.WebControls.Button) cc; //再所以这里两行根本不会执行
          bt.Enabled = false;
       }
    }
    }
      

  6.   

    foreach(System.Web.UI.Control control in this.Panel1.Controls)
    {
    switch(control.GetType().Name)
    {
    case "TextBox":
    TextBox textBox = (TextBox)control;
    textBox.Enabled = false;
    break;
    case "Button":
    Button button = (Button)control;
    button.Enabled = false;
    break;
    }
    }
      

  7.   

    加Case还可以加上对其他控件的控制,测试通过。