【问题标题】:Disable asp.net radiobutton with javascript使用 javascript 禁用 asp.net 单选按钮
【发布时间】:2010-09-07 03:01:30
【问题描述】:

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

【问题讨论】:

    标签: c# javascript asp.net


    【解决方案1】:

    我找到了 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" 这并不理想,因为它允许用户摆弄这些值,但在我的场景中很好。第二种解决方案是模仿禁用行为,详细信息可以在这里找到:http://aspnet.4guysfromrolla.com/articles/012506-1.aspx'>http://aspnet.4guysfromrolla.com/articles/012506-1.aspx

    【讨论】:

      【解决方案2】:

      在我的脑海中,我认为您必须检查复选框的“类型”属性以确定它是否是单选按钮。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2013-01-26
        • 2020-05-09
        • 2016-05-03
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多