User Name Password Register
DaniWeb IT Discussion Community
All
What is DaniWeb IT Discussion Community?
You're currently browsing the ASP.NET section within the Web Development category of DaniWeb, a massive community of 392,318 software developers, web developers, Internet marketers, and tech gurus who are all enthusiastic about making contacts, networking, and learning from each other. In fact, there are 2,837 IT professionals currently interacting right now! Registration is free, only takes a minute and lets you enjoy all of the interactive features of the site.
Please support our ASP.NET advertiser: Lunarpages ASP Web Hosting
Views: 2212 | Replies: 5
Reply
Join Date: Sep 2005
Posts: 2
Reputation: scr is an unknown quantity at this point 
Rep Power: 0
Solved Threads: 0
scr scr is offline Offline
Newbie Poster

counting checkboxes

  #1  
Sep 20th, 2005
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.
AddThis Social Bookmark Button
Reply With Quote  
Join Date: Dec 2004
Posts: 1,589
Reputation: tgreer is an unknown quantity at this point 
Rep Power: 7
Solved Threads: 34
Colleague
tgreer tgreer is offline Offline
Made Her Cry

Re: counting checkboxes

  #2  
Sep 20th, 2005
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;
  }
}
Reply With Quote  
Join Date: Sep 2005
Posts: 2
Reputation: scr is an unknown quantity at this point 
Rep Power: 0
Solved Threads: 0
scr scr is offline Offline
Newbie Poster

Re: counting checkboxes

  #3  
Sep 20th, 2005
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?
Reply With Quote  
Join Date: Dec 2004
Posts: 1,589
Reputation: tgreer is an unknown quantity at this point 
Rep Power: 7
Solved Threads: 34
Colleague
tgreer tgreer is offline Offline
Made Her Cry

Re: counting checkboxes

  #4  
Sep 20th, 2005
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.
Reply With Quote  
Join Date: Sep 2005
Posts: 8
Reputation: akadez is an unknown quantity at this point 
Rep Power: 0
Solved Threads: 0
akadez akadez is offline Offline
Newbie Poster

Re: counting checkboxes

  #5  
Oct 1st, 2005
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]
Reply With Quote  
Join Date: Nov 2005
Location: Mountain View, CA
Posts: 47
Reputation: kedar_challa is an unknown quantity at this point 
Rep Power: 3
Solved Threads: 1
kedar_challa's Avatar
kedar_challa kedar_challa is offline Offline
Light Poster

Re: counting checkboxes

  #6  
Nov 9th, 2005
Hi,

can u try this.
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
Reply With Quote  
Reply

Only community members can participate in forum threads. You must register or log in to contribute.

Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)

 

DaniWeb ASP.NET Marketplace
Thread Tools Display Modes

Similar Threads
Other Threads in the ASP.NET Forum

All times are GMT -4. The time now is 10:39 pm.
Forum system based on vBulletin Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
©2003 - 2008 DaniWeb® LLC