One of the most request scenarios, using a DataGrid, in ASP.Net is how to use Checkboxes with a DataGrid, and how to iterate through that selection, and get information about the checked item.
This sample shows how to check all items, un-check all items, and how to select several items, and return information from the DataGrid, based on each item checked. Notice here, that we’re using the FindControl metod to locate the Checkboxes – and check to see if each
Checkbox is Checked (cb.Checked), in order to accomplish this scenario.
One last thing – check out the ‘Kill Ending’ function. Since we’re creating a list, we are adding a comma after every checked item found. Therefore, at the end of the string we’re building, there will be an extra comma. This function removes that comma at the end of the string.
Here, we use the Authors table in the Pubs database, using SQL Server.
<%@ Import Namespace="System.Data" %>
<%@ Import Namespace="System.Data.SQLClient" %>
<script language="VB" Runat="server">
Dim ordList as String=""
Sub Page_Load(Source as Object, E as EventArgs)
if not Page.IsPostBack then
Dim strConn as string = "server=YourServer;uid=yourUID;pwd=yourPWD;database=Authors"
Dim MySQL as string = "Select Top 10 au_id, au_fname + '' '' + au_lname " & _
"as FullName from Authors"
Dim MyConn as New SQLConnection(strConn)
Dim ds as DataSet=New DataSet()
Dim Cmd as New SQLDataAdapter(MySQL,MyConn)
Cmd.Fill(ds,"PBAuthors")
dgPublishers.Datasource=ds.Tables("PBAuthors").DefaultView
dgPublishers.DataBind()
end if
End Sub
Sub doSelectAll(Source as Object, E as EventArgs)
Dim dgItem As DataGridItem
Dim chkDGrid As System.Web.UI.WebControls.CheckBox
For Each dgItem In dgPublishers.Items
chkDGrid = dgItem.FindControl("chk1")
chkDGrid.Checked = "True"
Next
End Sub
Sub doClearAll(Source as Object, E as EventArgs)
Dim dgItem As DataGridItem
Dim chkDGrid As System.Web.UI.WebControls.CheckBox
For Each dgItem In dgPublishers.Items
chkDGrid = dgItem.FindControl("chk1")
chkDGrid.Checked = "False"
Next
label1.text=""
End Sub
Sub GetAuthorIDs(Source as Object, E as EventArgs)
Dim i As Integer
For i = 0 To dgPublishers.Items.Count - 1
Dim dgItem As DataGridItem = dgPublishers.Items(i)
Dim cb As CheckBox = CType(dgItem.FindControl("chk1"), CheckBox)
If Not (cb Is Nothing) And cb.Checked Then
ordList+=dgItem.Cells(1).Text & ","
End If
Next i
label1.text=KillEnding(Trim(ordList))
End Sub
Function KillEnding(sItem as String)
Return sItem.Substring(0,sItem.Length-1)
End Function
</script>
<html>
<head>
<meta name="GENERATOR" Content="ASP Express 4.0">
<title>Using Checkboxes with a DataGrid</title>
<STYLE TYPE="text/css">
<!--
BODY {
font-family : Verdana;
font-size : 11pt;
}
-->
</STYLE>
</head>
<body>
<form id="form1" Runat="server">
<div align="center">
<asp:Label ID="label1" ForeColor="Blue" Font-Bold="True" Runat="server" />
<asp:Datagrid Runat="server"
Id="dgPublishers"
GridLines="Both"
cellpadding="0"
cellspacing="0"
Headerstyle-BackColor="#BDCFE7"
Headerstyle-Font-Name="Arial"
Headerstyle-Font-Size="8"
BackColor="#E7EFFF"
Font-Name="Arial"
Font-Size="8"
BorderColor="Black"
AutogenerateColumns="False">
<Columns>
<asp:TemplateColumn HeaderText="Select">
<ItemTemplate>
<asp:Checkbox id="chk1" Runat="server" />
</ItemTemplate>
</asp:TemplateColumn>
<asp:BoundColumn DataField="au_id" HeaderText="Author ID">
</asp:BoundColumn>
<asp:BoundColumn DataField="FullName" HeaderText="Author Name">
</asp:BoundColumn>
</Columns>
</asp:DataGrid>
<asp:Button id="btnSelectAll"
Text="Select All" onclick="doSelectAll" Runat="server" />
<asp:Button id="btnClearAll"
Text="Clear All" onclick="doClearAll" Runat="server" />
<asp:Button id="btnAuthorIDs"
Text="Get Author IDs" onclick="GetAuthorIDs" Runat="server" />
</div>
</form>
</body>
</html>