Question

Suppose I have 2 radio buttons r1 and r2, both the radio buttons ask for your gender, you can be a man or woman.

So what I want: if user checks r1 but then realizes that she is a woman, she then wants to check r2 so the control on r2 gets checked while r1 gets unchecked.

 <tr>
     <td>
         <asp:Label runat="server"  text="Chooose Your Category" ID="lblcategory"></asp:Label>
     </td>
     <td>
         <asp:RadioButton runat="server" Text="Male" ID="rbgold" />
     </td>
     <td>
         <asp:RadioButton runat="server" Text="Female" ID="rbsilver" /> 
     </td>
 </tr>

What should I do next so as I can choose only one?

Thanks in advance.

Was it helpful?

Solution 4

I got my answer by using an asp:RadioButtonList

<tr>
    <td>
        <asp:Label runat="server"  text="Chooose Your Category" ID="lblcategory">
        </asp:Label>
    </td>
    <td class="style1">
        <asp:RadioButtonList ID="rbgold" runat="server" 
                    RepeatColumns="2"
                    Width="200px">
            <asp:ListItem Text="Silver class" value="1" ></asp:ListItem>
            <asp:ListItem Text="Gold class" value="2"></asp:ListItem>
        </asp:RadioButtonList>   
    </td>    
</tr>

OTHER TIPS

Just give the two asp:RadioButtons the same GroupName

As MSDN notes,

Use the GroupName property to specify a grouping of radio buttons to create a mutually exclusive set of controls. You can use the GroupName property when only one selection is possible from a list of available options.

When this property is set, only one RadioButton in the specified group can be selected at a time.

Example:

<tr>
    <td>
        <asp:Label runat="server"  text="Chooose Your Category" ID="lblcategory">
        </asp:Label>
    </td>
    <td>
        <asp:RadioButton runat="server" Text="Male" ID="rbgold" GroupName="GenderGroup" />
    </td>
    <td>
        <asp:RadioButton runat="server" Text="Female" ID="rbsilver" GroupName="GenderGroup" /> 
    </td>
</tr>

You need to put them in the same group so that only one can be selected at a time, something like:

<asp:RadioButton runat="server" Text="Male"   ID="rbgold"   GroupName="xyzzy" />
<asp:RadioButton runat="server" Text="Female" ID="rbsilver" GroupName="xyzzy" />

Raghav Chopra: There is no need of RadioButtonList for this issue,you only put the radio buttons in same group then only one is selected at a time,and also your problem is solved

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top