Click here to Skip to main content
15,867,308 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi,

I have a problem that I am faced with, I have overcome 90% of the issue.

I have a checkboxlist that has a few items, if 1 specific value is selected then the rest of the checkboxes must not be selected.

the value that causes this condition is at position 6 in the list and it works fine for the items before it, but the items after position can still be selected.

C#
bool SA_selected = cblCountryAdd.Items[6].Selected;
            if (SA_selected)
            {
                for (int i2 = 0; i2 < cblCountryAdd.Items.Count; i2++)
                {
                    if (cblCountryAdd.SelectedValue != "ZA")
                    {
                        cblCountryAdd.Items[i2].Selected = false;
                    }
                    if (cblCountryAdd.Items[i2].Value == "ZI")
                    {
                        cblCountryAdd.Items[i2].Selected = false;
                    }
                    //else
                    //{
                    //   cblCountryAdd.Items[i2].Selected = false;
                    //}
                }
Posted
Comments
Andy Lanng 17-Mar-15 8:21am    
Why not use an OptionGroup? It's almost the same as a checkboxlist but by design you can only select one option.
Kats2512 17-Mar-15 8:27am    
sometimes multiple items can be selected in my case
Andy Lanng 17-Mar-15 14:19pm    
So you want to disable and uncheck the other options? All your code does atm is uncheck the other options.
Er. Puneet Goel 17-Mar-15 8:40am    
you can better handle this from jqyery.
Kats2512 17-Mar-15 8:51am    
can you show me a snippet of some jquery code please, hardly used jquery in my time

Hello,
I don't know whether your logic is correct or not, but it seems to be a problem with your string comparisons. Try changing operator != and == to !string.Equals and string.Equals:

bool SA_selected = cblCountryAdd.Items[6].Selected;
            if (SA_selected)
            {
                for (int i2 = 0; i2 < cblCountryAdd.Items.Count; i2++)
                {
                    if (!"ZA".Equals((String)cblCountryAdd.SelectedValue))
                    {
                        cblCountryAdd.Items[i2].Selected = false;
                    }
                    if ("ZI".Equals((String)cblCountryAdd.Items[i2].Value))
                    {
                        cblCountryAdd.Items[i2].Selected = false;
                    }
                }


More about String comparisons: http://stackoverflow.com/questions/1659097/why-would-you-use-string-equals-over[^]
 
Share this answer
 
Try this jQuery sample and adapt it to your need:
XML
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="WebForm15.aspx.cs" Inherits="WebApplicationCS2.WebForm15" %>

<!DOCTYPE html>

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
    <script src="http://ajax.googleapis.com/ajax/libs/jquery/1.11.2/jquery.min.js"></script>
<script>
    $(document).ready(function () {

        var $checkboxlist = $("#<%= CheckBoxList1.ClientID %>");

        // get the selector for the unique checkbox
        var $uniqueItemCheckbox = $checkboxlist.find(" input:checkbox[value='Unique Item']");

        // get selectors for all checkboxes other than the unique one.
        var $otherCheckbox = $checkboxlist.find("input[type=checkbox]").not($uniqueItemCheckbox);
        $uniqueItemCheckbox.change(function () {
            if ($(this).prop("checked")) {
                $otherCheckbox.prop(
                    "checked", false
                );
            }
            $otherCheckbox.prop(
                "disabled", $(this).prop("checked")
            );
       });
   });
    </script>
</head>
<body>
    <form id="form1" runat="server">
    <div>

        <asp:CheckBoxList ID="CheckBoxList1" runat="server">
            <asp:ListItem>Item 1</asp:ListItem>
            <asp:ListItem>Unique Item</asp:ListItem>
            <asp:ListItem>Item 3</asp:ListItem>
            <asp:ListItem>Item 4</asp:ListItem>
        </asp:CheckBoxList>

    </div>
    </form>
</body>
</html>

Learn jQuery Tutorial[^]
 
Share this answer
 
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900