2008-08-28 97 views
2

我试图禁用一堆JavaScript控件(以便它们回发值)。除了我的单选按钮,所有的控件都能正常工作,因为它们失去了价值。在通过递归函数调用以禁用所有子控件的下面的代码中,第二个else(else if(控件是RadioButton))从未被击中,并且RadioButton控件被识别为Checkbox控件。使用javascript禁用asp.net单选按钮

private static void DisableControl(WebControl control) 
    { 

     if (control is CheckBox) 
     { 
      ((CheckBox)control).InputAttributes.Add("disabled", "disabled"); 

     } 
     else if (control is RadioButton) 
     { 

     } 
     else if (control is ImageButton) 
     { 
      ((ImageButton)control).Enabled = false; 
     } 
     else 
     { 
      control.Attributes.Add("readonly", "readonly"); 
     } 
    } 

两个问题:
1.如何识别控制是一个单选按钮?
2.如何禁用它以便将其值回传?

回答

3

我发现了2种方法来使这个工作,下面的代码正确区分RadioButton和复选框控件。

private static void DisableControl(WebControl control) 
    { 
     Type controlType = control.GetType(); 

     if (controlType == typeof(CheckBox)) 
     { 
      ((CheckBox)control).InputAttributes.Add("disabled", "disabled"); 

     } 
     else if (controlType == typeof(RadioButton)) 
     { 
      ((RadioButton)control).InputAttributes.Add("disabled", "true"); 
     } 
     else if (controlType == typeof(ImageButton)) 
     { 
      ((ImageButton)control).Enabled = false; 
     } 
     else 
     { 
      control.Attributes.Add("readonly", "readonly"); 
     } 
    } 

而且我用的解决方案是设置在不理想的表单元素SubmitDisabledControls =“真”,因为它允许用户与价值观乱动,但在我的情况很好。第二种解决方案是模仿残疾人行为,细节可以在这里找到:http://aspnet.4guysfromrolla.com/articles/012506-1.aspx'>http://aspnet.4guysfromrolla.com/articles/012506-1.aspx

0

关闭我的头顶,我认为你必须检查复选框的“类型”属性,以确定它是否是一个单选按钮。