Button is disabled until a radio button is selected.

Dani AI

Generated

Nice answers from and the workaround mentioned. If you have a lot of radios, the easiest way to keep things tidy is: wire all radios to one place, and compute the state based on whether any radio is checked. That way you do not need to parse control names or duplicate code, and the button stays correct even if you add or remove options later.

VB.NET WinForms (radios inside GroupBox1, submit Button1):

' Run once (e.g., form load) to hook everything and set initial state.
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
    For Each c As Control In GroupBox1.Controls
        Dim rb = TryCast(c, RadioButton)
        If rb IsNot Nothing Then AddHandler rb.CheckedChanged, AddressOf UpdateButtonState
    Next
    UpdateButtonState(Nothing, EventArgs.Empty)
End Sub

Private Sub UpdateButtonState(sender As Object, e As EventArgs)
    Dim anyChecked As Boolean = False
    For Each c As Control In GroupBox1.Controls
        Dim rb = TryCast(c, RadioButton)
        If rb IsNot Nothing AndAlso rb.Checked Then anyChecked = True : Exit For
    Next
    Button1.Enabled = anyChecked
End Sub

If this thread is actually about ASP.NET Web Forms (tags suggest it), prefer a RadioButtonList and enable the button on the client to avoid postbacks. Keep IDs stable with ClientIDMode="Static".

<asp:RadioButtonList ID="rbl" runat="server" ClientIDMode="Static">
    <asp:ListItem>Option A</asp:ListItem>
    <asp:ListItem>Option B</asp:ListItem>
</asp:RadioButtonList>

<asp:Button ID="btnSubmit" runat="server" ClientIDMode="Static" Enabled="false" />

<script>
(function () {
  function updateBtn() {
    var radios = document.querySelectorAll("#rbl input[type=radio]");
    var any = false;
    for (var i = 0; i < radios.length; i++) { if (radios[i].checked) { any = true; break; } }
    document.getElementById("btnSubmit").disabled = !any;
  }
  document.addEventListener("change", function (e) { if (e.target && e.target.type === "radio") updateBtn(); });
  document.addEventListener("DOMContentLoaded", updateBtn);
})();
</script>

Notes:

  • Call the update method once at startup to enforce the default disabled state.
  • If you use UpdatePanel, reattach the handler after partial postbacks or hook into the MS AJAX load event.

Recommended Answers

All 6 Replies

1 RadioButton, 1 Button

Private Sub RadioButton1_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles RadioButton1.CheckedChanged
        If RadioButton1.Checked Then Button1.Enabled = True Else Button1.Enabled = False
    End Sub

that just returns the condition for 1 radiobutton i have like 16 radiobuttons

Do I have to make another guess and assume that you only have 2 Buttons?

See if this helps.

Private Sub myCoolRadioButtons_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) _
                                                Handles RadioButton1.CheckedChanged, RadioButton2.CheckedChanged, RadioButton3.CheckedChanged '// etc..
        '// get # from RadioButton#.
        Dim x As String = CType(sender, RadioButton).Name.Substring(CType(sender, RadioButton).Name.IndexOf("n") + 1)
        '// enable/disable the Button that ends with the same # as your RadioButton.
        CType(Me.Controls("Button" & x), Button).Enabled = CType(sender, RadioButton).Checked
    End Sub

If that code is a little too complicated, you can use the following code sample.

Private Sub myCoolRadioButtons_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) _
                                                Handles RadioButton1.CheckedChanged, RadioButton2.CheckedChanged, RadioButton3.CheckedChanged '// etc..
        If RadioButton1.Checked Then Button1.Enabled = True Else Button1.Enabled = False
        If RadioButton2.Checked Then Button2.Enabled = True Else Button2.Enabled = False
        If RadioButton3.Checked Then Button3.Enabled = True Else Button3.Enabled = False
        '// etc.
    End Sub

Haha ma bad :( not in a good mood some kid reported my username and it got changed.. >.>

So yeah that works fine tho i used another method,

made checkedchanged for each radio button, and sets a text in a textbox and by default btn1 is disabled.
on textchanged to textbox btn1 is enabled :)

Haha ma bad :( not in a good mood some kid reported my username and it got changed.. >.>...

Sorry to hear such, is it ok if I move on with my life now?:D

Indeed you do that sir :D

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.