【问题标题】:Can't determine status of checkbox in Grid View无法确定网格视图中复选框的状态
【发布时间】:2013-02-11 14:50:05
【问题描述】:

我有一个带有复选框的 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>

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

【问题讨论】:

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


    【解决方案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 访问单元格值会失败。

    达米安。

    【讨论】:

      【解决方案2】:

      试试这个,它在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

      【讨论】:

      • 我刚刚试过它仍然没有让我得到所选行的单元格值。它宁愿处理所有(如果我把它放在 if/else 语句之外)或者如果它在里面则不处理
      • 为什么 yo9u 放Cells[3] 我们不检查总是出现在网格视图第一列的复选框的值?
      • 如果你想要选择的行应该有类似:GridView1.SelectedRows。这需要 GridView 将 FullrowSelect 作为选择模式。
      • 我更新了硬编码值(适合我的程序,而不是你的)以使用文本“CheckBox1”作为单元格索引。
      • 对不起 Wozze 但是如果我用gvr 代替r,工作室抱怨它不是Web.UI.controls.TableCell 的成员在我的for 循环中,我使用的是GridView 而不是DataGridView。会不会有问题。 ASP 项目不包含 DataGridView
      【解决方案3】:

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

       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
      

      让我知道它是否有效

      【讨论】:

        猜你喜欢
        • 2011-03-06
        • 1970-01-01
        • 1970-01-01
        • 2012-03-04
        • 2011-08-06
        • 1970-01-01
        相关资源
        最近更新 更多