Home > Software design >  About RadioButtonList in ASP.NET
About RadioButtonList in ASP.NET

Time:01-18

I have a RadioButtonList. I want the button to be available when I click "Agree", and not available when I click "Disagree". Default is disabled.

Now whether I agree or disagree, my btnSubmit button will become available. And it won't change back to unavailable.

function acceptprotocol() {
    if (document.getElementById("rblAccept").SelectValue == 0 {
        document.getElementById("btnSubmit").disabled = true;
    }
    else {
        document.getElementById("btnSubmit").disabled = false;
    }
}

RadioButtonList:

<asp:RadioButtonList ID="rblAccept" runat="server" 
     RepeatDirection="Horizontal" style="margin:auto" 
     onclick="acceptprotocol()">
    <asp:ListItem Value="0">Agree</asp:ListItem>
    <asp:ListItem Value="1" Selected="True">Against</asp:ListItem>
</asp:RadioButtonList>

How to solve it? Please help me.

CodePudding user response:

Here's the code snippet for your solution:

RadioButtonList:

<asp:RadioButtonList ID="rblAccept" runat="server" RepeatDirection="Horizontal" style="margin:auto"
 onclick="acceptprotocol()">
<asp:ListItem Value="0">Agree</asp:ListItem>
<asp:ListItem Value="1" Selected="True">Disagree</asp:ListItem>
</asp:RadioButtonList>
<asp:Button ID="btnSubmit" runat="server" Text="Submit" Enabled="false" />

Function:

function acceptprotocol() {
        var radioButtonAccept = document.getElementById('<%=rblAccept.ClientID %>').getElementsByTagName("input");;
        for (var i = 0; i < radioButtonAccept.length; i  ) {
            if (radioButtonAccept[i].checked) {
                if (radioButtonAccept[i].value == 1) {
                    document.getElementById('<%=btnSubmit.ClientID %>').disabled = true;
                }
                else {
                    document.getElementById('<%=btnSubmit.ClientID %>').disabled = false;
                    //use this bellow line if you want the after enable button button will availble all time
                    document.getElementById('<%=rblAccept.ClientID %>').removeAttribute("onclick");
                }
                break;
            }
            else {
                document.getElementById('<%=btnSubmit.ClientID %>').disabled = true;
            }
        }
    }
  • Related