最初,我们在标记页面上有一个checkboxlist控件,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从checkboxlist切换到复选框。
我很感激帮助重写上面的代码,以便从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
答案 0 :(得分:0)
遍历GridView的行,并使用FindControl方法获取对复选框的引用。
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(0).FindControl("recs"), CheckBox).Text & " from the db."
End If
Next