Solved

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

Posted on 2008-10-04
2
635 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
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

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Creating an analog clock UserControl seems fairly straight forward.  It is, after all, essentially just a circle with several lines in it!  Two common approaches for rendering an analog clock typically involve either manually calculating points with…
Many of us here at EE write code. Many of us write exceptional code; just as many of us write exception-prone code. As we all should know, exceptions are a mechanism for handling errors which are typically out of our control. From database errors, t…
Two types of users will appreciate AOMEI Backupper Pro: 1 - Those with PCIe drives (and haven't found cloning software that works on them). 2 - Those who want a fast clone of their boot drive (no re-boots needed) and it can clone your drive wh…
This video shows how to quickly and easily add an email signature for all users on Exchange 2016. The resulting signature is applied on a server level by Exchange Online. The email signature template has been downloaded from: www.mail-signatures…

820 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