最初,我们在标记页面上有一个复选框列表控件,ID="recs"
在该标记上,我们选中所有/取消选中所有复选框。这样,用户可以选中一个或多个复选框。
我们可以使用以下代码获取复选框的值:
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
这对我们来说非常有效。
然而,今天早上,我们被要求在标记中添加更多列。事实证明这是不可能的。
我们决定使用 gridview 从复选框列表切换到复选框。
我将不胜感激协助重写上述代码以从 gridview 的复选框中获取选中的项目。
Gridview 代码片段如下。
<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