****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
|