用JavaScript禁用asp.net单选按钮
时间:2020-03-05 18:44:21 来源:igfitidea点击:
我正在尝试使用JavaScript禁用一堆控件(以便它们回传值)。除了我的单选按钮失去了价值之外,所有控件都工作正常。在下面的代码中,该代码通过递归函数调用以禁用所有子控件,第二个else(否则,如果(控件为RadioButton
),则不会被点击),并且RadioButton控件被标识为Checkbox
控件。
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.如何禁用它以使其回传其值?
解决方案
回答
我想,我们必须检查复选框的" type"属性,以确定它是否是单选按钮。
回答
我发现有两种方法可以使其正常工作,下面的代码正确区分了RadioButton和Checkbox控件。
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"); } }
我使用的解决方案是在form元素中设置SubmitDisabledControls =" True",这不是理想的选择,因为它允许用户摆弄值,但在我的方案中很好。第二种解决方案是模仿"残疾人"行为,可以在此处找到详细信息:http://aspnet.4guysfromrolla.com/articles/012506-1.aspx'>http://aspnet.4guysfromrolla.com/articles/012506-1 .aspx。