Monday, March 26, 2012

loop through all checkboxes

Is their a way to loop through all the checkboxes on a submitted form?

i think im close with this:

for(i = 0; i < CheckBox.Count; i++){
if (((CheckBox)FindControl("chkTestDI_" + i)).Checked)
Response.Write("chkTestDI_" + i);
// how do i get the value?

}For i = 0 To CheckBoxList.Items.Count - 1
If CheckBoxList.Items(i).Selected Then
UrValue= CheckBoxList.Items(i).Value
End If
Next
here is the code

for(i = 0; i < CheckBoxList.Items.Count; i++){
if(CheckBoxList.Items(i).Selected){
Response.Write(CheckBoxList.Items(i).Value);
}
}

the error:

Compiler Error Message: CS0120: An object reference is required for the nonstatic field, method, or property 'System.Web.UI.WebControls.ListControl.Items'

this is what the code looks like when rendered to the page:

<p>
<input type='checkbox' name='chkTestDI:7' value='7' id='chkTestDI_7'>
 Item A<br>
<input type='checkbox' name='chkTestDI:8' value='8' id='chkTestDI_8'>
 Item B<br>
<input type='checkbox' name='chkTestDI:1' value='1' id='chkTestDI_1'>
 Item C<br>
<input type='checkbox' name='chkTestDI:5' value='5' id='chkTestDI_5'>
 Item D*<br>

<select name='Afil1'>
<option value="">- Please select affiliate -</option>
<option value="5517">ABC Affiliate</option>
<option value="5487">adsfdsfasdfffdddfd</option>
<option value="5512">Affiliate ABC</option>
<option value="5515">Affiliate test x</option>
</select>
<br>
<input type='checkbox' name='chkTestDI:2' value='2' id='chkTestDI_2'>
 Item E*<br>

<select name='Afil1'>
<option value="">- Please select affiliate -</option>
<option value="5517">ABC Affiliate</option>
<option value="5487">adsfdsfasdfffdddfd</option>
<option value="5512">Affiliate ABC</option>
<option value="5515">Affiliate test x</option>
</select>
<br>
<input type='checkbox' name='chkTestDI:3' value='3' id='chkTestDI_3'>
 Item F </p>
Check out the MSDN-article:Top Questions about the DataGrid Web Server Control. It also covers what you ask for.

0 comments:

Post a Comment