Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to group checkboxes to restrict selection to a single option?

I'm trying to set up two check box's in a DataGrid so that only one can be checked at any one time.

At the moment, the following renders the existing state of the choice on screen:

<asp:TemplateColumn HeaderText="Choice One">
    <ItemTemplate>
        <asp:CheckBox ID="CheckBoxChoiceOne" 
                      CommandArgument='<%# DataBinder.Eval(Container.DataItem, "ChoiceOne") %>'
                     runat="server">
        </asp:CheckBox>
    </ItemTemplate>
</asp:TemplateColumn>
    <asp:TemplateColumn HeaderText="Choice Two">
    <ItemTemplate>
          <asp:CheckBox ID="CheckBoxChoiceTwo" 
                        CommandArgument='<%# DataBinder.Eval(Container.DataItem, "ChoiceTwo") %>'
                        runat="server">
          </asp:CheckBox>
    </ItemTemplate>
</asp:TemplateColumn>

How do I go about ensuring that if the user chooses ChoiceOne that any selection of ChoiceTwo will be unselected and vice versa? Is there any way to state in the DataGrid control that these two checkboxes are grouped together?

like image 551
mezoid Avatar asked Jan 25 '26 21:01

mezoid


1 Answers

This is a situation for Radio Buttons instead of checkboxes. You are violating user expectations if you have checkboxes behave in that manner. From a UI perspective that isn't a great idea.

like image 52
Matt Avatar answered Jan 27 '26 10:01

Matt



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!