I am facing problem in finding the checked items in checkbox list. Actually the list items for the checkbox list are loaded from database. But by using the code below i am not able to find the checked item in the list and the items are always returning false. Below is my code Can someone please help me with this?
protected void GetCheckboxlist_Click(object sender, EventArgs e)
{
string s = string.Empty;
for (int i = 0; i < CheckBoxList1.Items.Count; i++)
{
if (CheckBoxList1.Items[i].Selected)
{
// List the selected items
s = s + CheckBoxList1.Items[i].Text + ",";
}
}
}
Your code looks fine to me but try to use Linq
instead;
IEnumerable<string> CheckedItems = CheckBoxList1.Items.Cast<ListItem>()
.Where(i => i.Selected)
.Select(i => i.Value);
After that, you can add your s
string these values like;
foreach(string i in CheckedItems)
s += i + ",";
Don't forget to add System.Linq
namespace.