gpt4 book ai didi

c# - 使用 javascript 禁用 asp.net 单选按钮

转载 作者:行者123 更新时间:2023-11-29 20:26:53 24 4
gpt4 key购买 nike

我正在尝试使用 JavaScript 禁用一堆控件(以便它们回发值)。除了我的单选按钮,所有控件都工作正常,因为它们失去了值(value)。在下面的代码中,通过递归函数调用以禁用所有子控件,第二个 else (else if (control is 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.如何识别哪个控件是radiobutton?
2. 如何禁用它以便它回传它的值?

最佳答案

我找到了 2 种方法让它工作,下面的代码正确区分了 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");
}
}

我使用的解决方案是在表单元素中设置 SubmitDisabledControls="True",这并不理想,因为它允许用户摆弄这些值,但在我的场景中没问题。第二种解决方案是模仿禁用行为,详细信息可在此处找到:https://web.archive.org/web/20210608183803/http://aspnet.4guysfromrolla.com/articles/012506-1.aspx'> https://web.archive.org/web/20210608183803/http://aspnet.4guysfromrolla.com/articles/012506-1.aspx .

关于c# - 使用 javascript 禁用 asp.net 单选按钮,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32173/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com