CheckBoxList like RadioButtonList

Posted on 2005-05-04
Last Modified: 2012-06-27
I want to use a checkbox list and make it function like a radiobutton list (ie - make only one selection possible) instead of multiple selections.

Would like to have the look and feel of a checkbox list with a radiobuttonlist functionality.

Any thoughts.

Question by:steve_mick972
    LVL 6

    Assisted Solution

    Nope you can't do that directly. You will have to write javascript to do this, when you check check a box you will have to find if there is any other checkbox checked, if so uncheck it.
    LVL 6

    Accepted Solution

    you can do it directly, just with a bit of coding behind the scenes - try this on:
    first, put this function into your code:

      Private Function CheckBoxes(ByVal Box As CheckBox) As Boolean
        CheckBoxes = False
        Dim myControl As Control
        Dim myForm As HtmlForm
        Dim myCheckBox As CheckBox
        Dim selectedBox As String
        Dim Form1 As String = "Form1"
        myForm = CType(Page.FindControl(Form1), HtmlForm)
        For Each myControl In myForm.Controls
          If myControl.GetType.ToString = "System.Web.UI.WebControls.CheckBox" Then
            myCheckBox = CType(myControl, CheckBox)
            If myCheckBox.Checked = True And myCheckBox.ID <> Box.ID Then
              myCheckBox.Checked = False
            End If
          End If
      End Function

    Next, create your textboxes, set the AutoPostBack attribute to "True" and on the CheckedChanged event handler, add the following code:

     Private Sub CheckBox1_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles CheckBox1.CheckedChanged
        If CheckBox1.Checked = True Then
          CheckBox1.Checked = True
        End If
    End Sub

    Replacing all the 'CheckBox1's with the name of your checkbox.
    Works for me :)

    LVL 6

    Expert Comment

    oh, by the way, that code will cycle through all controls on the screen, so if you want to have other check boxes that aren't in your 'Check Box List', then amend the function line:

    ...  If myCheckBox.Checked = True And myCheckBox.ID <> Box.ID Then ...

    to include other 'ids' that you don't want to include. e.g.

    ...  If myCheckBox.Checked = True And myCheckBox.ID <> Box.ID And myCheckBox.ID <> chkBlah.ID Then ...


    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    What Security Threats Are You Missing?

    Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

    In this Article, I will provide a few tips in problem and solution manner. Opening an ASPX page in Visual studio 2003 is very slow. To make it fast, please do follow below steps:   Open the Solution/Project. Right click the ASPX file to b…
    One of the pain points with developing AJAX, JavaScript, JQuery, and other client-side behaviors is that JavaScript doesn’t allow for cross domain request for pulling content. For example, JavaScript code on could not pull conte…
    Migrating to Microsoft Office 365 is becoming increasingly popular for organizations both large and small. If you have made the leap to Microsoft’s cloud platform, you know that you will need to create a corporate email signature for your Office 365…
    This video discusses moving either the default database or any database to a new volume.

    761 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    9 Experts available now in Live!

    Get 1:1 Help Now