Small problem with my code

S

Steven

Please look at this code .. this will add the values of all 'checked' boxes
(check box list control) into an array .. the problem is its adding the
values twice.
for eg: If I selected 2, 3 on the page the array is getting values ..
2,2,3,3. It should be getting 2,3. What is the error?

foreach ( ListItem item in LayersCheckBoxList.Items )
{
int j = LayerNameList1.Count ;
if ( item.Selected )
{
for (int i =0; i<j; i++ )
{
if (LayersCheckBoxList.Items.Selected)
{
LayerNameList2.Add(LayersCheckBoxList.Items);
}
}
}
}

Any help would be greatly appreciated !

Thanks
Steven
 
P

Peter Rilling

Not sure what LayerNameList1 and LayerNameList2 are in the context of this
code.

What is the purpose of the inner loop? If you simply want to add them to an
array, then why not something like:

ArrayList al = new ArrayList();
foreach( ListItem item in LayersCheckBoxList.Items )
{
if( item.Selected )
{
al.Add(item);
}
}
 
S

Steven

Thanks Peter .. it works .. !

regards
steven

Peter Rilling said:
Not sure what LayerNameList1 and LayerNameList2 are in the context of this
code.

What is the purpose of the inner loop? If you simply want to add them to
an
array, then why not something like:

ArrayList al = new ArrayList();
foreach( ListItem item in LayersCheckBoxList.Items )
{
if( item.Selected )
{
al.Add(item);
}
}

Steven said:
Please look at this code .. this will add the values of all 'checked' boxes
(check box list control) into an array .. the problem is its adding the
values twice.
for eg: If I selected 2, 3 on the page the array is getting values ..
2,2,3,3. It should be getting 2,3. What is the error?

foreach ( ListItem item in LayersCheckBoxList.Items )
{
int j = LayerNameList1.Count ;
if ( item.Selected )
{
for (int i =0; i<j; i++ )
{
if (LayersCheckBoxList.Items.Selected)
{
LayerNameList2.Add(LayersCheckBoxList.Items);
}
}
}
}

Any help would be greatly appreciated !

Thanks
Steven

 

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