Ev.
Ev.

Reputation: 7579

Radiobutton in Group (GroupName) are all checked

So I've got three RadioButtons, They're not in a RadioButtonList because I need to add some textboxes next to each of them.

I've added a GroupName, and on the front end they behave as expected. ONLY ONE appears checked at a time.

However in the code, if I do:

RadioButton1.Checked = true;
RadioButton2.Checked = true;
RadioButton3.Checked = true;

I would expect only the last one, RadioButton3, to be checked, because they all belong to the same group. This is not the case. All three evaluate to true.... how can that be?

I have to set them explicitly to false... am I missing something?

Upvotes: 2

Views: 1913

Answers (1)

Kelsey
Kelsey

Reputation: 47766

I think this is the correct behavior although it is not what you might expect.

Consider that a RadioButton is just a CheckBox with some extended functionality to automatically to give that exclusive checking functionality. In the background it is still a checkbox though. See the hierarchy from MSDN:

System.Object
    System.Web.UI.Control
        System.Web.UI.WebControls.WebControl
            System.Web.UI.WebControls.CheckBox
                System.Web.UI.WebControls.RadioButton

The output has all items with the attribute checked="checked" output for the input of type="radio". Eg:

<input id="rad1" type="radio" name="Test" value="rad1" checked="checked" /><label for="rad1">1</label><br />
<input id="rad2" type="radio" name="Test" value="rad2" checked="checked" /><label for="rad2">2</label><br />
<input id="rad3" type="radio" name="Test" value="rad3" checked="checked" /><label for="rad3">3</label>

From the Checked property documenation:

Checked (inherited from CheckBox): Gets or sets a value indicating whether the CheckBox control is checked.

So the Checked property acts just like the CheckBox version with no functionality included to look for other controls in the same group and remove them which makes sense since it is a singular control.

Upvotes: 1

Related Questions