Question : Problem getting the Selected value of a CheckBoxList (used in a gridview)

I have a gridview. Each row has a checkbox list that is bound to a datasource. Whenever I uncheck/check an item I want to run the selectedindexchanged to do some database stuff. For some reason, even when I UNCHECK the checkbox selected is always true. Any idea why?

    Protected Sub cblFolders_SelectedIndexChanged(ByVal sender As Object, ByVal e As System.EventArgs)
        Dim cbl As New CheckBoxList
        cbl = CType(sender, CheckBoxList)
       
        If cbl.SelectedItem.Selected Then    ' ALWAYS TRUE

Answer : Problem getting the Selected value of a CheckBoxList (used in a gridview)

After what's been covered so far the answer is... there's not enough information about how you're laying this up to see what's wrong...

This evaluation, [ If cbl.SelectedItem.Selected Then ], will result in 2 possible states:
1) True
2) Error because cbl.SelectedItem is Nothing
*** Its kindof stupid because it will never be FALSE. For it to be FALSE the item is not selected, and therefore cannot be a "SelectedItem". If this doesn't make sense then you'll just have to play with it some to see what I mean.

The only way it is TRUE after you uncheck all items in a row is if something earlier in the page life cycle is affecting it before we get to the SelectedIndexChanged event or there is a problem with the databinding implementation for the checkboxlist.

I've put together a small sample that works. It is lazy code I know, but it does and demonstrates exactly what you are trying to do, and what I've stated above. You have to hookup your own data to it, but anything will do. Ill show the data its attached to for my demo.

I expect if you make a simplified version of what you are doing you will figure out what is wrong, or at least you'll have something you can post here that we can work with to see what's wrong. I would say show the gridview and the databinding, but other code could be affecting as well. So I really suggested making a dummy version for testing. My guesses are:

1) There is re-binding going on (I know you've already considered this).
2) You are injecting the checkboxlist as a dynamic control into the gridview, and this implementation is causing the wonkiness.
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
18:
19:
20:
21:
22:
23:
24:
25:
26:
27:
28:
29:
30:
31:
32:
33:
34:
35:
36:
37:
38:
39:
40:
41:
42:
43:
44:
45:
46:
47:
48:
49:
50:
51:
52:
53:
54:
55:
56:
57:
58:
59:
60:
61:
62:
63:
64:
65:
66:
67:
68:
69:
70:
71:
72:
73:
74:
75:
76:
77:
78:
79:
80:
81:
82:
83:
84:
85:
86:
87:
88:
89:
90:
91:
92:
93:
94:
95:
96:
97:
98:
99:
100:
101:
102:
103:
104:
****ASPX PAGE ****
<%@ Page Language="VB" AutoEventWireup="false" CodeFile="Default.aspx.vb" Inherits="_Default" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title>Grid Proto</title>
</head>
<body>
    <form id="form1" runat="server">
		<span>SelectedIndex1:</span><asp:Label ID="SelectedIndex1" runat="server" /><br />
		<span>Display Error:</span><asp:Label ID="lblError" runat="server" /><br />
		<asp:GridView ID="GridView1" runat="server">
			<Columns>
				<asp:TemplateField>
					<ItemTemplate>
						<asp:CheckBoxList ID="cblBound" runat="server" AutoPostBack="True" 
							onselectedindexchanged="cblBound_SelectedIndexChanged"></asp:CheckBoxList>
					</ItemTemplate>
				</asp:TemplateField>
			</Columns>
		</asp:GridView>
    </form>
</body>
</html>


****ASPX.VB CODE BEHIND****
Imports System.Data.SqlClient

Partial Class _Default
    Inherits System.Web.UI.Page

    Public Const connStr As String = "Data Source=.\SQLEXPRESS;Integrated Security=True"

    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        If Not IsPostBack Then
            BindGrid()
        End If
    End Sub



    Private Sub BindGrid()
        Dim conn As New SqlConnection(connStr)
        Dim cmd As New SqlCommand("SELECT * FROM DevTable", conn)
        conn.Open()
        Dim dr As SqlDataReader = cmd.ExecuteReader
        GridView1.DataSource = dr
        GridView1.DataBind()
        dr.Close()
        conn.Close()
        conn.Dispose()
    End Sub


    Protected Sub GridView1_RowDataBound(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewRowEventArgs) Handles GridView1.RowDataBound
        If e.Row.RowType = DataControlRowType.DataRow Then
            Dim cbl As CheckBoxList = e.Row.FindControl("cblBound")
            Dim conn As New SqlConnection(connStr)
            Dim cmd As New SqlCommand("SELECT testID, fName FROM DevTable", conn)
            conn.Open()
            Dim dr As SqlDataReader = cmd.ExecuteReader
            cbl.DataSource = dr
            cbl.DataValueField = "testID"
            cbl.DataTextField = "fName"
            cbl.DataBind()
            dr.Close()
            conn.Close()
            conn.Dispose()
        End If
    End Sub

    Protected Sub cblBound_SelectedIndexChanged(ByVal sender As Object, ByVal e As System.EventArgs)

        Dim cbl As CheckBoxList = DirectCast(sender, CheckBoxList)

        If cbl.SelectedItem Is Nothing Then
            SelectedIndex1.Text = "Nothing Selected"
        Else
            SelectedIndex1.Text = cbl.SelectedItem.Selected
        End If

        Try
            If cbl.SelectedItem.Selected Then
                lblError.Text = cbl.SelectedItem.Text
            End If
        Catch ex As Exception
            lblError.Text = ex.Message
        End Try

    End Sub
End Class


*****SIMPLE DATABASE TABLE ****

testID   fName    lName    nName    sDate
1	bill	blast	bblast	NULL
2	jason	jamaih	jjamaih	NULL
3	kang	bak	kbak	NULL
4	roger 	rabbit	rrabbit	NULL
NULL	NULL	NULL	NULL	NULL
Random Solutions  
 
programming4us programming4us