CheckBoxList like RadioButtonList

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.

Thanks.
LVL 1
steve_mick972Asked:
Who is Participating?
 
mcgantsConnect With a Mentor Commented:
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
    Next
  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
      CheckBoxes(CheckBox1)
    Else
      CheckBox1.Checked = True
    End If
End Sub

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

cheers,
mcg
0
 
dharmesh_amityConnect With a Mentor Commented:
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.
0
 
mcgantsCommented:
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 ...

cheers,
mcg
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.