Small problem with my code

  • Thread starter Thread starter Steven
  • Start date Start date
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
 
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);
}
}
 
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

 
Back
Top