Click here to Skip to main content
15,904,416 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
can anyone tell me how to findout which checkbox item is checked in checkedListBox?
I have to find out which item is checked in checkedlistbox and depending on that assign value to a variable.
Posted

use this

C#
for (int i = 0; i < chlDeptList.Items.Count; i++)
            {
                if (chlDeptList.Items[i].Selected)
                    deptids += chlDeptList.Items[i].Value+",";
            }
 
Share this answer
 
Comments
Rakhil Naik 1-Aug-13 5:59am    
thanks
but it showing error for Selected in if statement
rahul-4u 1-Aug-13 6:02am    
how you assign data to checkedlistbox
Rakhil Naik 1-Aug-13 6:05am    
i dnt get u
rahul-4u 1-Aug-13 6:09am    
you have items in chlDeptList.Items. How these items are attached to chlDeptList. Also post the error you are getting.
Rakhil Naik 1-Aug-13 6:15am    
checkedListBox1
Serial no
first name
last name
addr

if serial no is checked i have to assign value 'a1' to variable chk1 in same way i have to check for First name,last name,address an assign 'a2' to chk2, 'a3'to chk3, 'a4' to chk4
Hi

You can try this

C#
public static string GetSelectedItems(System.Web.UI.WebControls.CheckBoxList chkList)
{
            StringBuilder sSelectedItems=new StringBuilder();
            for (int i = 0; i < chkList.Items.Count; i++)
            {
                if (chkList.Items[i].Selected)
                    sSelectedItems.Append(chkList.Items[i].Value + ",");
            }
            return sSelectedItems.ToString().TrimEnd(',');
}


It is to get all the selected items in the CheckBoxList.
 
Share this answer
 
v3
Comments
Adarsh chauhan 1-Aug-13 5:58am    
good one.. +5
Rakhil Naik 1-Aug-13 6:04am    
thanks
i want to check each checkbox one by one and assign value to a variable if it is checked otherwise keep it empty?
can u help me wth this
Thomas ktg 1-Aug-13 6:19am    
public void GetSelectedItems()
{
for (int i = 0; i < chkList.Items.Count; i++)
{
if (chkList.Items[i].Selected)
chkList.Items[i].Text = "Value 1";
else
chkList.Items[i].Text = "";
}
}
Rakhil Naik 1-Aug-13 7:19am    
it is showing some error for selected and Text in your code.
ERROR: "object does not contain defination for selected"
Thomas ktg 1-Aug-13 7:23am    
First of all you should bind the CheckboxList. Then you try this would work.
Hello,

If you want to get those value client side, then you can use JQuery as follows

$(function() {
$('#input[type="checkbox"]:checked').each(function() {
$('#result').append(', '+$(this).val());
});
});
 
Share this answer
 
C#
protected void chkList_SelectedIndexChanged(object sender, EventArgs e)
    {
        string sNo = "10202";
        string sFName = "Thomas";
        string sSName = "A";
        string sAddress = "India";

        List<string> collections = new List<string>();
        collections.Add(sNo);
        collections.Add(sFName);
        collections.Add(sSName);
        collections.Add(sAddress);

        for (int i = 0; i < chkList.Items.Count; i++)
        {
            if (chkList.Items[i].Selected)
                chkList.Items[i].Value = collections[i];
            else
                chkList.Items[i].Value = "";
        }
    }


<asp:CheckBoxList ID="chkList" AutoPostBack="true" runat="server"
onselectedindexchanged="chkList_SelectedIndexChanged">
<asp:ListItem Selected="True">Serial No</asp:ListItem>
<asp:ListItem>First Name</asp:ListItem>
<asp:ListItem>Last Name</asp:ListItem>
<asp:ListItem>Address</asp:ListItem>
</asp:CheckBoxList>
 
Share this answer
 
v3
Comments
Rakhil Naik 1-Aug-13 8:39am    
it is showing error for "selected" in "if (chkList.Items[i].Selected)"
Thomas ktg 1-Aug-13 8:42am    
Give me the HTML code for the CheckListBox Control. And code you've written.
Rakhil Naik 1-Aug-13 8:50am    
i am writting this code for windows application

private void checkedListBox1_SelectedIndexChanged(object sender, EventArgs e)
{
string chk = "a1";


List<string> collections = new List<string>();
collections.Add(chk);


for (int i = 0; i < checkedListBox1.Items.Count; i++)
{
if (checkedListBox1.Items[i].Selected)
checkedListBox1.Items[i].Value = collections[i];
else
checkedListBox1.Items[i].Value = "";
}


}
it is showing error for "selected" in "if (chkList.Items[i].Selected)" and "Value" in
chkList.Items[i].Value = collections[i];
else
chkList.Items[i].Value = "";
Rakhil Naik 1-Aug-13 9:15am    
it showing this error
ERROR:'object' does not contain a definition for 'selected' and no extension method 'selected' accepting a first argument of type 'object' could be found (are you missing a using directive or an assembly reference?)

same error it showing for Value.
can u plz help me out with this error?
Thomas ktg 2-Aug-13 3:01am    
Try this. But this is not the exact solution you are expecting to do. You can modify this as your needs.
private void checkedListBox1_ItemCheck(object sender, ItemCheckEventArgs e)
{
List<string> collectionsValue = new List<string>();
collectionsValue.Add("10202");
collectionsValue.Add("Thomas");
collectionsValue.Add("A");
collectionsValue.Add("India");


List<string> collectionsItems = new List<string>();
collectionsItems.Add("Serial No");
collectionsItems.Add("First Name");
collectionsItems.Add("Second Name");
collectionsItems.Add("Address");

if (e.NewValue == CheckState.Checked)
{
checkedListBox1.Items[e.Index] = collectionsValue[e.Index];
}
else
{
checkedListBox1.Items[e.Index] = collectionsItems[e.Index];
}
}

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