Allow only one checkbox to be checked in ASP.NET

Bill Johnson 11 Reputation points
2023-02-13T10:26:54.7966667+00:00

Hi,
I have a panel with checkboxes:

<asp:Panel runat="server" ID="pnlMyPanel" GroupingText="My Panel Test" Visible="false">

  <div class="DetailsBoxSetting">
    <asp:CheckBox ID="chk_One" runat="server" CssClass="chk" />
    <asp:Label ID="lbl_one" CssClass="StandardLabel" runat="server">One</asp:Label>
  </div>  
                              
  <div class="DetailsBoxSetting">
    <asp:CheckBox ID="chk_Two" runat="server" CssClass="chk" />
    <asp:Label ID="lbl_Two" CssClass="StandardLabel" runat="server">Two</asp:Label>
  </div> 
                               
</asp:Panel>

I want to allow only one checkbox to be checked .
How do I do that ?
Thank you

Developer technologies ASP.NET Other
{count} votes

2 answers

Sort by: Most helpful
  1. Olaf Helper 47,436 Reputation points
    2023-02-13T10:39:37.1566667+00:00

    I want to allow only one checkbox to be checked .

    Use a RadioButton Class instead.

    1 person found this answer helpful.
    0 comments No comments

  2. Lan Huang-MSFT 30,186 Reputation points Microsoft External Staff
    2023-02-14T03:13:44.06+00:00

    Hi @Bill Johnson ,

    I think you can use javascript to allow only one checkbox to be checked. onclick="javascript:UnCheckOtherCheckBox(this, 'chk_Two');" in chk_One. onclick="javascript:UnCheckOtherCheckBox(this, 'chk_One');" in chk_Two.

    <script type="text/javascript">
           function UnCheckOtherCheckBox(chkbox1, chkbox2) {
               if (chkbox1.checked) {
                   //Uncheck the other checkbox
                   document.getElementById(chkbox2).checked = false;
               }
           }
       </script>
    
     <asp:Panel runat="server" ID="pnlMyPanel" GroupingText="My Panel Test">
                    <div class="DetailsBoxSetting">
                        <asp:CheckBox ID="chk_One" runat="server" CssClass="chk" onclick="javascript:UnCheckOtherCheckBox(this, 'chk_Two');" />
                        <asp:Label ID="lbl_one" CssClass="StandardLabel" runat="server">One</asp:Label>
                    </div>
                    <div class="DetailsBoxSetting">
                        <asp:CheckBox ID="chk_Two" runat="server" CssClass="chk" onclick="javascript:UnCheckOtherCheckBox(this, 'chk_One');" />
                        <asp:Label ID="lbl_Two" CssClass="StandardLabel" runat="server">Two</asp:Label>
                    </div>
                </asp:Panel>
    

    1

    Best regards,

    Lan Huang


    If the answer is the right solution, please click "Accept Answer" and kindly upvote it. If you have extra questions about this answer, please click "Comment".
    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.

    0 comments No comments

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.