Monday, March 26, 2012

loop through checkboxes to get selected items

Hello,

I have a list of checkboxes and would like to grab what the user selected.

the code below works for binding the checkboxes via Checkboxlist

for(i = 0; i < chkTestDI.Items.Count; i++)
{
if(chkTestDI.Items[i].Selected) {
sb.Append(" " + chkTestDI.Items[i].Text + "<br>");
}
}

i created my checkboxes via a datareader due to some custom things i had to do.

is there a way i can lopp through the choices? this is what i have. I tried to mimic what .NET does on the fly and i get this error message:
Compiler Error Message: CS0246: The type or namespace name 'chkTestDI' could not be found (are you missing a using directive or an assembly reference?)

<table id="chkTestDI" cellspacing="0" cellpadding="2" border="0" style="background-color:White;border-width:0px;font-size:10pt;width:100%;border-collapse:collapse;">
<tr>
<td><input type='checkbox' name='chkTestDI:7' value='7' id='chkTestDI_7' onClick="return validate(this.form);">
 Monday<br></td>
</tr>
<tr>
<td><input type='checkbox' name='chkTestDI:8' value='8' id='chkTestDI_8' onClick="return validate(this.form);">
 Tuesday<br></td>
</tr>
<tr>
<td><input type='checkbox' name='chkTestDI:1' value='1' id='chkTestDI_1' onClick="return validate(this.form);">
 Wednesday<br></td>
</tr>
<tr>
<td><input type='checkbox' name='chkTestDI:5' value='5' id='chkTestDI_5' onClick="return validate(this.form);">
 Thursday<br> <br></td>
</tr>
<tr>
<td><input type='checkbox' name='chkTestDI:2' value='2' id='chkTestDI_2' onClick="return validate(this.form);">
 Friday<br> <br></td>
</tr>
</table>
</span>May be this link helps u ....

http://support.microsoft.com/default.aspx?scid=kb;EN-US;q321881

Cheers,
Devendra Naik.
The last value of i in your loop is (chkTestDI.Items.Count). Try (chkTestDI.Items.Count-1). Because the index starts from Zero your last index should be Count minus 1.

0 comments:

Post a Comment