c# - Disable asp.net radiobutton with javascript - Stack Overflow

I'm trying to disable a bunch of controls with JavaScript (so that they post back values). All the

I'm trying to disable a bunch of controls with JavaScript (so that they post back values). All the controls work fine except for my radio buttons as they lose their value. In the below code which is called via a recursive function to disable all child controls the Second else (else if (control is RadioButton)) is never hit and the RadioButton control is identified as a Checkbox control.

    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");
        }
    }

Two Questions:
1. How do I identify which control is a radiobutton?
2. How do I disable it so that it posts back its value?

I'm trying to disable a bunch of controls with JavaScript (so that they post back values). All the controls work fine except for my radio buttons as they lose their value. In the below code which is called via a recursive function to disable all child controls the Second else (else if (control is RadioButton)) is never hit and the RadioButton control is identified as a Checkbox control.

    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");
        }
    }

Two Questions:
1. How do I identify which control is a radiobutton?
2. How do I disable it so that it posts back its value?

Share Improve this question edited Dec 3, 2015 at 19:45 Siyual 16.9k8 gold badges47 silver badges60 bronze badges asked Aug 28, 2008 at 13:03 NicholasNicholas 1,7505 gold badges22 silver badges31 bronze badges
Add a ment  | 

2 Answers 2

Reset to default 3

I found 2 ways to get this to work, the below code correctly distinguishes between the RadioButton and Checkbox controls.

    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");
        }
    }

And the solution I used is to set SubmitDisabledControls="True" in the form element which is not ideal as it allows a user to fiddle with the values but is fine in my scenario. The second solution is to mimic the Disabled behavior and details can be found here: https://web.archive/web/20210608183803/http://aspnet.4guysfromrolla./articles/012506-1.aspx'>https://web.archive/web/20210608183803/http://aspnet.4guysfromrolla./articles/012506-1.aspx.

Off the top of my head, I think you have to check the "type" attribute of the checkbox to determine if it's a radio button.

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745316261a4622232.html

相关推荐

  • c# - Disable asp.net radiobutton with javascript - Stack Overflow

    I'm trying to disable a bunch of controls with JavaScript (so that they post back values). All the

    7小时前
    10

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信