Solved

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

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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

It’s quite interesting for me as I worked with Excel using vb.net for some time. Here are some topics which I know want to share with others whom this might help. First of all if you are working with Excel then you need to Download the Following …
Calculating holidays and working days is a function that is often needed yet it is not one found within the Framework. This article presents one approach to building a working-day calculator for use in .NET.
I designed this idea while studying technology in the classroom.  This is a semester long project.  Students are asked to take photographs on a specific topic which they find meaningful, it can be a place or situation such as travel or homelessness.…
This is a video that shows how the OnPage alerts system integrates into ConnectWise, how a trigger is set, how a page is sent via the trigger, and how the SENT, DELIVERED, READ & REPLIED receipts get entered into the internal tab of the ConnectWise …

919 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

Need Help in Real-Time?

Connect with top rated Experts

15 Experts available now in Live!

Get 1:1 Help Now