2008-08-28 95 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

關閉我的頭頂,我認爲你必須檢查複選框的「類型」屬性,以確定它是否是一個單選按鈕。