Johnny
Johnny

Reputation: 21

Choose 1 checkbox out of 2

I got several checkbox, but I only want the the text "New Desktop" and "New Laptop" can only choose 1 out of 2. I hope it could be done in C#.

<asp:CheckBoxList ID="Service1" runat="server" 
                     Width="251px" >
                 <%--onselectedindexchanged="checkBox1_CheckedChanged"--%>
                    <asp:ListItem text="New Login ID & Email Address" ></asp:ListItem>
                    <asp:ListItem text="New Desktop" Value="2" oncheckedchanged="checkBox1_CheckedChanged" ></asp:ListItem>
                    <asp:ListItem text="New Notebook" Value="3" oncheckedchanged="checkBox2_CheckedChanged"></asp:ListItem>
                    <asp:ListItem text="New Mouse"></asp:ListItem>
                    <asp:ListItem text="New Keyboard"></asp:ListItem>
                    <asp:ListItem text="New Printer"></asp:ListItem>
                </asp:CheckBoxList>


//.cs pages
protected void checkBox1_CheckedChanged(object sender, EventArgs e)
        {
            if (Service1.Items[2].Selected == true)
            {
                Service1.Items[3].Enabled = false;
            }
        }
protected void checkBox2_CheckedChanged(object sender, EventArgs e)
        {
            if (Service1.Items[3].Selected == true)
            {
                Service1.Items[2].Enabled = false;
            }
        }

Upvotes: 1

Views: 252

Answers (2)

ITSGuru
ITSGuru

Reputation: 194

You can use OnSelectedIndexChanged event for CheckBoxList with AutoPostBack="True" so your control send request to server and selection chenged event will be call I.e : For design

<asp:CheckBoxList ID="Service1" runat="server" Width="251px" AutoPostBack="True" OnSelectedIndexChanged="Service1_SelectedIndexChanged">
        <asp:ListItem Text="New Login ID & Email Address"></asp:ListItem>
        <asp:ListItem Text="New Desktop" Value="2"></asp:ListItem>
        <asp:ListItem Text="New Notebook" Value="3" ></asp:ListItem>
        <asp:ListItem Text="New Mouse"></asp:ListItem>
        <asp:ListItem Text="New Keyboard"></asp:ListItem>
        <asp:ListItem Text="New Printer"></asp:ListItem>
    </asp:CheckBoxList>

and in code: 'li' have all selected item and as per your requirement "New Desktop" and "New Laptop" only choose 1 out of 2 .

protected void Service1_SelectedIndexChanged(object sender, EventArgs e)
        {
            CheckBoxList li = (CheckBoxList)sender;
            foreach (ListItem l in li.Items)
            {
                if (l.Value == "2")
                {
                    if (l.Selected)
                    {
                        Service1.Items[2].Enabled = false;
                    }
                    else
                    {
                        Service1.Items[2].Enabled = true;
                    }

                }
                else if (l.Value == "3")
                {
                    if (l.Selected)
                    {
                        Service1.Items[1].Enabled = false;
                    }
                    else
                    {
                        Service1.Items[1].Enabled = true;
                    }
                }
            }
        }

Upvotes: 1

Charan Ghate
Charan Ghate

Reputation: 1394

I am giving you one example below but still this may not be a feasible way. You need to add CheckedChanged event to each CheckBox to remove check from other CheckBoxes

protected void CheckBox1_CheckedChanged(object sender, EventArgs e)
{  
     if(CheckBox1.Checked)
     {
          CheckBox2.Checked =false;
          CheckBox3.Checked =false;
          CheckBox4.Checked =false;
      }
}

You should repeat the above evevt for CheckBox2 , CheckBox3 and so on. You can extend it as per your requirement.

But in this scenario I would recommend you to use the ASP.NET Radio Button Control. Please refer this link for more information on Radio Button Control.

Upvotes: 1

Related Questions