Solved

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

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

Enabling OSINT in Activity Based Intelligence

Activity based intelligence (ABI) requires access to all available sources of data. Recorded Future allows analysts to observe structured data on the open, deep, and dark web.

Join & Write a Comment

Today I had a very interesting conundrum that had to get solved quickly. Needless to say, it wasn't resolved quickly because when we needed it we were very rushed, but as soon as the conference call was over and I took a step back I saw the correct …
A long time ago (May 2011), I have written an article showing you how to create a DLL using Visual Studio 2005 to be hosted in SQL Server 2005. That was valid at that time and it is still valid if you are still using these versions. You can still re…
Internet Business Fax to Email Made Easy - With eFax Corporate (http://www.enterprise.efax.com), you'll receive a dedicated online fax number, which is used the same way as a typical analog fax number. You'll receive secure faxes in your email, fr…
You have products, that come in variants and want to set different prices for them? Watch this micro tutorial that describes how to configure prices for Magento super attributes. Assigning simple products to configurable: We assigned simple products…

758 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

17 Experts available now in Live!

Get 1:1 Help Now