CheckBoxList Problems

P

Philip Townsend

I am having some difficulty getting a checkboxlist control to determine
which checkboxes are checked. Here is a code sample from the event that
should read it:

private void btnFilter_Click(object sender, System.EventArgs e)
{
ArrayList arr=new ArrayList();
for(int i=0;i<ckFilter.Items.Count;i++)
{
if(ckFilter.Items.Selected)
{
arr.Add(ckFilter.Items.Text);
}
}
IEnumerator en=arr.GetEnumerator();
while(en.MoveNext())
{
Response.Write(en.Current+"<br>");
}
}


The only problem is that ckFilter.Items.Selected always returns false
regardless of whether the box is checked. I am sure this is a simple
problemAny ideas?
 
S

Stoitcho Goutsev \(100\) [C# MVP]

Hi Philip,
I assume that ck is the checked list box objet and I don't see how you can
compile this code. CheckedListBox.Items collectio is collection of objects
and as objects they don't have Text properties. So, I assume that you put in
the checked list box objects of some class, which I call Foo and Foo has
property Text.
If you want to find cheked items only you can do the following
private void btnFilter_Click(object sender, System.EventArgs e)
{
foreach(Foo item in ckFilter.SelectedItems)
{
Response.Write(item.Text + "<br>");

}

--
HTH
B\rgds
100
Philip Townsend said:
I am having some difficulty getting a checkboxlist control to determine
which checkboxes are checked. Here is a code sample from the event that
should read it:

private void btnFilter_Click(object sender, System.EventArgs e)
{
ArrayList arr=new ArrayList();
for(int i=0;i<ckFilter.Items.Count;i++)
{
if(ckFilter.Items.Selected)
{
arr.Add(ckFilter.Items.Text);
}
}
IEnumerator en=arr.GetEnumerator();
while(en.MoveNext())
{
Response.Write(en.Current+"<br>");
}
}


The only problem is that ckFilter.Items.Selected always returns false
regardless of whether the box is checked. I am sure this is a simple
problemAny ideas?
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top