DaniWeb IT Discussion Community

DaniWeb IT Discussion Community (http://www.daniweb.com/forums/)
-   ASP.NET (http://www.daniweb.com/forums/forum18.html)
-   -   counting checkboxes (http://www.daniweb.com/forums/thread32648.html)

scr Sep 20th, 2005 6:15 pm
counting checkboxes
 
All,
i have a bunch of dynamic checkboxes created in a datagrid, and i have the results going to a database when they are checked. question is how do i count the total # of checkboxes checked. here is the code i have that loops through the checkboxes to submit to the db.

foreach (DataGridItem spriden_ID in dgEmployees.Items)
{
CheckBox cbSpriden = (CheckBox) spriden_ID.FindControl("cbSpriden");
if (cbSpriden.Checked)
{
//Stuff to add to db here
}
else
{
lblStatus.Text ="Scheduling failed: " + addCourses.ErrorMessage;
}
}

Once the update to the DB succeeds i want to display the count of all the checkboxes submitted..how would i do this?? thanks in advance for any help.

tgreer Sep 20th, 2005 7:19 pm
Re: counting checkboxes
 
Coudn't you just add a counter to your logic?

int myCounter = 0;
foreach (DataGridItem spriden_ID in dgEmployees.Items)
{
  CheckBox cbSpriden = (CheckBox) spriden_ID.FindControl("cbSpriden");
  if (cbSpriden.Checked)
  {
    //Stuff to add to db here
    myCounter++;
  }
  else
  {
    lblStatus.Text ="Scheduling failed: " + addCourses.ErrorMessage;
  }
}

scr Sep 20th, 2005 7:56 pm
Re: counting checkboxes
 
yes i tried that...however when i return the count..for example if i check 3 checkboxes...it will return 123. so it is counting them right...but i only need the 3...hehe. thank you for your response....any other clues?

tgreer Sep 20th, 2005 8:48 pm
Re: counting checkboxes
 
I'm sorry, I don't understand your reply. There is no way the code above would return the value "123" for the "myCounter" integer. If there are 3 checkboxes, the value would be "3" after the loop completes.

akadez Oct 1st, 2005 11:03 pm
Re: counting checkboxes
 
I agree with tgreer, his example would never return "123". The only thing I can think is that you are using a string as your counter variable and concatenating it rather than using an integer and incrementing it. Below is the only feasible code I can think of to duplicate your resuls (example is generic).

[HTML]string[] arr = new string[]{"a", "b", "c"};
string count = string.Empty;

for (int i = 0;i < arr.length; i++)
{
//do something with the string

//update counter
count += (i + 1);
}[/HTML]

kedar_challa Nov 9th, 2005 10:00 pm
Re: counting checkboxes
 
Hi,

can u try this.
Quote:

int myCounter = 0;
foreach (DataGridItem spriden_ID in dgEmployees.Items)
{
CheckBox cbSpriden = (CheckBox) spriden_ID.cells[index];
if (cbSpriden.Checked)
{
//Stuff to add to db here
myCounter++;
}
else
{
lblStatus.Text ="Scheduling failed: " + addCourses.ErrorMessage;
}
}



Thanks,
Kedar


All times are GMT -4. The time now is 2:33 pm.

Forum system based on vBulletin Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
©2003 - 2008 DaniWeb® LLC