ASP.NET中禁用服务器控件
功能描述:把一个Form里面的控件禁用,例外的控件可通过参数控制

 Code
Code public static void DisableInputControl(params Control[] excludeControls)
public static void DisableInputControl(params Control[] excludeControls)
 
         {
{ Page page = HttpContext.Current.Handler as Page;
            Page page = HttpContext.Current.Handler as Page; if (page == null)
            if (page == null) return;
                return;
 DisableChildInputControl(page, excludeControls);
            DisableChildInputControl(page, excludeControls); }
        }
 public static void DisableChildInputControl(Control control, params Control[] excludeControls)
        public static void DisableChildInputControl(Control control, params Control[] excludeControls)
 
         {
{ foreach (Control child in control.Controls)
            foreach (Control child in control.Controls)
 
             {
{ if (excludeControls.Length > 0)
                if (excludeControls.Length > 0)
 
                 {
{ bool exists = false;
                    bool exists = false; foreach (Control excludeControl in excludeControls)
                    foreach (Control excludeControl in excludeControls)
 
                     {
{ if (excludeControl.Equals(child))
                        if (excludeControl.Equals(child))
 
                         {
{ exists = true;
                            exists = true; break;
                            break; }
                        } }
                    } if (exists)
                    if (exists) continue;
                        continue; }
                } if (child is TextBox)
                if (child is TextBox)
 
                 {
{ ((TextBox)child).Enabled = false;
                    ((TextBox)child).Enabled = false; }
                } else if (child is Button)
                else if (child is Button)
 
                 {
{ ((Button)child).Enabled = false;
                    ((Button)child).Enabled = false; }
                } else if (child is DropDownList)
                else if (child is DropDownList)
 
                 {
{ ((DropDownList)child).Enabled = false;
                    ((DropDownList)child).Enabled = false; }
                } //递归
                //递归 DisableChildInputControl(child, excludeControls);
                DisableChildInputControl(child, excludeControls); }
            } }
        } 
                    
                     
                    
                 
                    
                 
 
                
            
         
         浙公网安备 33010602011771号
浙公网安备 33010602011771号