Solved

How to best count the checked checkboxes in a datagrid after each selection/deselection

Posted on 2008-10-04
2
636 Views
Last Modified: 2012-05-05
I am trying to keep a running total of the number of checkboxes selected in a datagrid. My problem is not in the coding but how to invoke the code after each selection/deselection?

The default event for the checkbox is
Protected Sub CheckBox_Select_CheckedChanged(ByVal sender As Object, ByVal e As System.EventArgs)

however this event is not triggered when a checkbox is changed.

any clues to why this would not be trigerred? could it be something to do with masterpages? or being inside an accordion control
' This code works if I request it from a button but the 
 
    Protected Sub CheckBox_Select_CheckedChanged(ByVal sender As Object, ByVal e As System.EventArgs)
        Dim cb As CheckBox
        Dim row As GridViewRow
        Dim contactCounter As Integer = 0
 
        For Each row In GridView1.Rows
            cb = CType(row.FindControl("CheckBox_Select"), CheckBox)
            If cb.Checked Then
                contactCounter = contactCounter + 1
            End If
        Next
        Label_contactsSelected.Text = contactCounter & " contacts selected"
    End Sub

Open in new window

0
Comment
Question by:JOHNFROG
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
2 Comments
 
LVL 2

Accepted Solution

by:
JasonChandler earned 500 total points
ID: 22640805
have you set the checkbox to autopostback="true"
for that even to be fired the checkbox must have autopostback = true
e.g
<asp:Checkbox id="Checkbox" runat="server" autopsotback="true"><asp:Checkbox>

The checkbox in your grid should be set up like this:
<asp:CheckBox ID="ckbox" Runat="server" AutoPostBack="True"
OnCheckedChanged="chkSelected_CheckedChanged">
</asp:CheckBox>

then in your code behind

Protected Sub chkSelected_CheckedChanged(ByVal sender As Object, ByVal e As EventArgs)
        Dim ck1 As CheckBox = CType(sender, CheckBox)
         'You now have access to the checkbox
          'Start adding code to count how many selected.
End Sub

The key here is that the event needs to be cought outside of the grid so the default event for the grid cant be fired so you have to set one up your self.
by using the "OnCheckedChanged="chkSelected_CheckedChanged">" you are telling the checkbox to fire a different event of which you can then use.

Hope this helps
0
 

Author Closing Comment

by:JOHNFROG
ID: 31503021
good one. thanks for that. i should have spotted that. works great now.
0

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This article describes relatively difficult and non-obvious issues that are likely to arise when creating COM class in Visual Studio and deploying it by professional MSI-authoring tools. It is assumed that the reader is already familiar with the cla…
More often than not, we developers are confronted with a need: a need to make some kind of magic happen via code. Whether it is for a client, for the boss, or for our own personal projects, the need must be satisfied. Most of the time, the Framework…
With Secure Portal Encryption, the recipient is sent a link to their email address directing them to the email laundry delivery page. From there, the recipient will be required to enter a user name and password to enter the page. Once the recipient …
Finds all prime numbers in a range requested and places them in a public primes() array. I've demostrated a template size of 30 (2 * 3 * 5) but larger templates can be built such 210  (2 * 3 * 5 * 7) or 2310  (2 * 3 * 5 * 7 * 11). The larger templa…

737 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question