0

我有一个带有复选框的 GridView。但是我在确定是否选中了给定行的复选框时遇到了真正的问题。

我需要从一行中检索某个值并将其放入代码中。但是当我遍历 GridView Rows 时,程序不会输入检查 checkBox'x 状态的 if 语句。

这是后端的代码:

 Dim Primaryid As String = "Initial stage"
    For Each gvr As GridViewRow In GridView1.Rows

        If (CType(gvr.FindControl("CheckBox1"), CheckBox)).Checked = True Then
            Primaryid = gvr.Cells(1).Text
        End If
    Next gvr

    Dim exmess As String = "alert('" & Primaryid & "')"
    Page.ClientScript.RegisterStartupScript(Me.GetType(), "ErrorAlert", exmess, True)

这是 GridView 的代码。我在加载页面时自动填充它:

<asp:GridView ID="GridView1" runat="server" CellPadding="4" ForeColor="#333333" 
        GridLines="None" Width="1500px">
        <Columns>

                    <asp:TemplateField >
                        <ItemTemplate>
                            <asp:CheckBox ID="CheckBox1" runat="server" />
                        </ItemTemplate>
                    </asp:TemplateField>

          </Columns>
        <AlternatingRowStyle BackColor="White" />
        <EditRowStyle BackColor="#2461BF" />
        <FooterStyle BackColor="#507CD1" Font-Bold="True" ForeColor="White" />
        <HeaderStyle BackColor="#507CD1" Font-Bold="True" ForeColor="White" />
        <PagerStyle BackColor="#2461BF" ForeColor="White" HorizontalAlign="Center" />
        <RowStyle BackColor="#EFF3FB" />
        <SelectedRowStyle BackColor="#D1DDF1" Font-Bold="True" ForeColor="#333333" />
        <SortedAscendingCellStyle BackColor="#F5F7FB" />
        <SortedAscendingHeaderStyle BackColor="#6D95E1" />
        <SortedDescendingCellStyle BackColor="#E9EBEF" />
        <SortedDescendingHeaderStyle BackColor="#4870BE" />
    </asp:GridView>

如果您能指出我的错误,我将不胜感激。

4

3 回答 3

1

您必须遍历所有行并找到您的复选框控件,然后检查其选中状态。查看工作示例(我使用在线转换器将其转换为 VB)

Protected Sub btnGetSelectedRows_Click(sender As Object, e As EventArgs)
Dim items = New StringBuilder()
For Each grow As GridViewRow In GridView1.Rows
    Dim chkTemp As CheckBox = DirectCast(grow.FindControl("chkSelectRow"), CheckBox)
    If chkTemp IsNot Nothing Then
        If chkTemp.Checked Then
            items.Append(String.Format("{0},", GridView1.DataKeys(grow.RowIndex)("ProductID").ToString()))
        End If
    End If
Next

If items.Length > 0 Then
        Response.Write("You selected Ids:" & Convert.ToString(items))
    End If

End Sub

和aspx

<asp:GridView ID="GridView1" runat="server" DataSourceID="SqlDataSource1" 
        AllowPaging="True" AutoGenerateColumns="False" DataKeyNames="ProductID">
        <Columns>
            <asp:TemplateField>
                <ItemTemplate>
                    <asp:CheckBox ID="chkSelectRow" runat="server" />
                </ItemTemplate>
            </asp:TemplateField>
            <asp:BoundField DataField="ProductID" HeaderText="ProductID" 
                InsertVisible="False" ReadOnly="True" SortExpression="ProductID" />
            <asp:BoundField DataField="ProductName" HeaderText="ProductName" 
                SortExpression="ProductName" />
            <asp:BoundField DataField="SupplierID" HeaderText="SupplierID" 
                SortExpression="SupplierID" />
            <asp:BoundField DataField="CategoryID" HeaderText="CategoryID" 
                SortExpression="CategoryID" />
            <asp:BoundField DataField="QuantityPerUnit" HeaderText="QuantityPerUnit" 
                SortExpression="QuantityPerUnit" />
            <asp:BoundField DataField="UnitPrice" HeaderText="UnitPrice" 
                SortExpression="UnitPrice" />
            <asp:BoundField DataField="UnitsInStock" HeaderText="UnitsInStock" 
                SortExpression="UnitsInStock" />
            <asp:BoundField DataField="UnitsOnOrder" HeaderText="UnitsOnOrder" 
                SortExpression="UnitsOnOrder" />
            <asp:BoundField DataField="ReorderLevel" HeaderText="ReorderLevel" 
                SortExpression="ReorderLevel" />
            <asp:CheckBoxField DataField="Discontinued" HeaderText="Discontinued" 
                SortExpression="Discontinued" />
            <asp:BoundField DataField="CategoryName" HeaderText="CategoryName" 
                SortExpression="CategoryName" />
        </Columns>
    </asp:GridView>
    <asp:SqlDataSource ID="SqlDataSource1" runat="server" 
        ConnectionString="<%$ ConnectionStrings:NorthwindConnectionString %>" 
        SelectCommand="SELECT * FROM [Alphabetical list of products]"></asp:SqlDataSource>
    <br />

注意,我使用了

数据键

属性来访问行的主键。从长远来看,当您将来更改 gridview 上的列时,我建议您执行与使用 cellIndex 访问单元格值相同的操作。

达米安。

于 2013-02-26T10:27:16.667 回答
0

试试这个,它适用于winforms。不能保证它会为你工作。我对单元格值进行了硬编码,应该可以按照您的方式获取单元格。

无论如何,主要思想是检查单元格的值,而不是尝试获取“检查”值(因为它们是相同的)。所以我基本上将单元格值解析为一个布尔对象。

For Each r As DataGridViewRow In DataGridView1.Rows
    Dim b As Boolean = False
    Boolean.TryParse(r.Cells("CheckBox1").Value, b)
    If b Then
        'Do stuff
    End If
Next

编辑:wops 在 C# 中做到了:F 给我一秒钟,我会发布 VB:P

于 2013-02-26T08:30:17.237 回答
0

试试这个可能对你有帮助。

 Dim Primaryid As String = "Initial stage"
    For Each row As GridViewRow In GridView1.Rows
        Dim chk As CheckBox = TryCast(row.Cells(0).Controls(0), CheckBox)

        If chk.Checked Then
        End If
    Next

让我知道它是否有效

于 2013-02-26T08:35:19.227 回答