Initially, we have a checkboxlist control on the markup page with ID=”recs”
On that markup, we have check all / uncheck all checkboxes. This way, users can check one checkbox or several checkboxes.
We were able get the value(s) of checked boxes with the following code:
Protected Sub btnGetCheck_Click(ByVal sender As Object, ByVal e As EventArgs)
Thread.Sleep(9000)
Dim uItems As String = ""
For i As Integer = 0 To recs.Items.Count - 1
If recs.Items(i).Selected Then
If uItems <> "" Then uItems = uItems & ","
uItems = uItems & _
"You checked" & recs.Items(i).Text & "from the db"
End If
Next
Response.Redirect("sendto.aspx?p=" & Server.UrlEncode(uItems))
End Sub
This worked real well for us.
However, this morning, we were asked to add more columns to the markup. It turns out that this was not possible.
We decided to switch from checkboxlist to checkboxs using gridview.
I would appreciate asssistance with rewriting the above code to get checked items from the checkboxes in gridview.
Gridview code snip is below.
<asp:GridView ID="GridView1" runat="server" HeaderStyle-CssClass = "header"
AutoGenerateColumns = "false" Font-Names = "Arial" OnRowDataBound = "RowDataBound"
Font-Size = "11pt" AlternatingRowStyle-BackColor = "#C2D69B" >
<Columns>
<asp:TemplateField>
<HeaderTemplate>
<asp:CheckBox ID="checkAll" runat="server" onclick = "checkAll(this);" />
</HeaderTemplate>
<ItemTemplate>
<asp:CheckBox ID="recs" runat="server" onclick = "Check_Click(this)" />
</ItemTemplate>
</asp:TemplateField>
<asp:BoundField ItemStyle-Width = "150px" DataField = "custid" HeaderText = "Customer ID" />
<asp:BoundField ItemStyle-Width = "150px" DataField = "firstname" HeaderText = "First Name" />
<asp:BoundField ItemStyle-Width = "150px" DataField = "lastname" HeaderText = "Last Name"/>
<asp:BoundField ItemStyle-Width = "150px" DataField = "os" HeaderText = "OS"/>
</Columns>
</asp:GridView>
<asp:Button ID="btnGetCheck" runat="server" Text="Get Checked Items" onclick="btnGetCheck_Click" /><br />
Dim uItems As String = String.Empty
For Each r As GridViewRow In GridView1.Rows
If CType(r.Cells(0).FindControl("recs"), CheckBox).Checked Then
If uItems <> String.Empty Then
uItems += ","
End If
uItems += "You checked " & CType(r.Cells(1).FindControl("recs"), CheckBox).Text & " from the db."
Response.Write(uItems)
Response.End()
End If
Iterate through the rows of the GridView and grab a reference to the checkboxes using the FindControl method.