无法确定网格视图中的复选框状态

时间:2013-02-26 08:12:05

标签: asp.net mysql vb.net gridview checkbox

我有一个带Checkbox的GridView。但是我确定是否检查了给定行的复选框是否存在真正的问题。

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

这是后端的代码:

 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>

如果你能指出我的错误,我将非常感激。

3 个答案:

答案 0 :(得分:1)

你必须遍历所有行和&amp;找到您的复选框控件,然后检查其已检查状态。查看工作示例(我使用在线转换器将其转换为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 />

注意,我已经使用了

  

DataKeys

用于访问行主键的属性。我建议你做同样的事情,就像访问单元格值一样,如果你将来在gridview上更改列时,cellIndex会失败。

达明。

答案 1 :(得分: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

答案 2 :(得分: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

让我知道它是否有效