?
Solved

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

Posted on 2008-10-04
2
Medium Priority
?
639 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 2000 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: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

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

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…
It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
NetCrunch network monitor is a highly extensive platform for network monitoring and alert generation. In this video you'll see a live demo of NetCrunch with most notable features explained in a walk-through manner. You'll also get to know the philos…
If you’ve ever visited a web page and noticed a cool font that you really liked the look of, but couldn’t figure out which font it was so that you could use it for your own work, then this video is for you! In this Micro Tutorial, you'll learn yo…
Suggested Courses

752 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