If you are looking at server side code then following code should do the trick:
private int mTotal;
private void EnumerateCheckBoxes(Control control)
{
if (control is CheckBox)
{
var check = (CheckBox)control;
if (check.Checked)
{
int value;
if (int.TryParse(check.Text, out value))
{
mTotal += value;
}
}
}
else if (control.HasControls())
{
foreach(var c in control.Controls)
{
EnumerateCheckBoxes(c);
}
}
}
protected void Page_Load(Object sender, EventArgs e)
{
mTotal = 0;
EnumerateCheckBoxes(this.Form);
textbox1.Text = mTotal.ToString();
}
Although, this is in C#, it should be easy to port to VB.NET. Also few other things to consider:
- This code will count radio buttons
as well as because it gets inherited
from CheckBox. If that is to be
avoided then replace
if (control is
CheckBox)
with if
(control.GetType() ==
typeof(CheckBox))
- If you wish to consider checkboxes
from CheckBoxList then you have to
write another condition to see if
control is CheckBoxList and then
within condition, enumerate items
withing checkboxlist. Items count to
be added to total count while
selected items to be added to
checked count.