如何在循环中获取GridViewRow中记录的ID?

How to get the ID of the record in the GridViewRow in loop?

目标:Gridview 每行都有一个复选框,然后用户单击一个按钮,所有勾选的行都会在按钮代码中起作用。我需要检查 CheckBox 的所有记录的 ID。

错误:

Object reference not set to an instance of an object.

行:ID_Current = row.FindControl("ID").ToString

如果我将 ID_Current = DirectCast(row.FindControl("cbSelect"), CheckBox).Checked 更改为 ID_Current = DirectCast(row.FindControl("cbSelect"), CheckBox).Checked,结果会得到 'True',但我已经知道并想要获取 ID。

Aspx 代码与我的 Gridview:

<asp:Panel ID="ActionGrid" runat="Server">
    <h2>Actions To Edit</h2>
    <asp:GridView ID="GridView3" runat="server" AllowPaging="True" AllowSorting="True" AlternatingRowStyle-CssClass="alt" AutoGenerateColumns="False" CssClass="mGrid" DataKeyNames="UPRN" DataSourceID="SqlDataSource3" EmptyDataText="There are no data records to display." Font-Size="Medium" PagerStyle-CssClass="pgr" Width="1000px">
        <%--  AutoGenerateEditButton="True"--%>
        <Columns>
            <asp:TemplateField HeaderText="Select">
                <ItemTemplate>
                    <asp:CheckBox ID="cbSelect" runat="server" AutoPostBack="false" />
                </ItemTemplate>
            </asp:TemplateField>
            <asp:BoundField ControlStyle-Width="50px" DataField="UPRN" HeaderText="UPRN" ReadOnly="True" SortExpression="UPRN" />
            <asp:BoundField DataField="LocationItemPosition" HeaderText="Location Item Position" ReadOnly="True" SortExpression="LocationItemPosition" />
            <asp:TemplateField HeaderText="Surveye" SortExpression="SurveyDate">
                <ItemTemplate>
                    <asp:Label ID="lblSurveyDate" runat="server" Text='<%# apFunctionCharacters.fncDateTidy(Eval("SurveyDate"))%>' />
                </ItemTemplate>
            </asp:TemplateField>

            <asp:BoundField DataField="ItemRef" HeaderText="Item Ref" ReadOnly="True" SortExpression="ItemRef" />
            <asp:BoundField DataField="OverallRiskCategory" HeaderText="Overall Risk Category" ReadOnly="True" SortExpression="OverallRiskCategory" />
            <asp:BoundField DataField="Comments" HeaderText="Comments" ReadOnly="True" SortExpression="Comments" />

        </Columns>
        <EditRowStyle BackColor="#999999" />
        <FooterStyle BackColor="#5D7B9D" Font-Bold="True" ForeColor="White" />
        <HeaderStyle BackColor="#5D7B9D" Font-Bold="True" ForeColor="White" />
        <PagerStyle BackColor="#284775" ForeColor="White" HorizontalAlign="Center" />
        <RowStyle BackColor="#F7F6F3" ForeColor="#333333" />
        <SelectedRowStyle BackColor="#E2DED6" Font-Bold="True" ForeColor="#333333" />
    </asp:GridView>
    <asp:Button ID="btnChangeToNA" runat="server" CssClass="Button" Text="Change to NA" />
    <asp:Label ID="lblTest" runat="server" Text="Label"></asp:Label>
</asp:Panel>

提交按钮:

<asp:Button ID="btnChangeToNA" runat="server" CssClass="Button" Text="Change to NA" />

.aspx.vb 存储 procedure/action 代码

Protected Sub btnChangeToNA_Click(sender As Object, e As EventArgs) Handles btnChangeToNA.Click
    For Each row As GridViewRow In GridView3.Rows

        Dim ID_Current As String = ""
        'read the label            
        If DirectCast(row.FindControl("cbSelect"), CheckBox).Checked Then
            ID_Current = row.FindControl("ID").ToString

            ' change value
            '############stored procedure here

            Dim connection As SqlConnection
            Dim command As New SqlCommand
            Dim ds As New DataSet
            Dim ConnectionString1 As String = System.Configuration.ConfigurationManager.ConnectionStrings("MY_SQL").ToString()
            connection = New SqlConnection(ConnectionString1)

            connection.Open()

            With command
                .Connection = connection
                .CommandText = "spChangeValue "
                .CommandType = CommandType.StoredProcedure
                .Parameters.Clear()
                .Parameters.AddWithValue("@ID_Current", ID_Current)
                .ExecuteNonQuery()
            End With
            '#############################
            lblTest.Text += ID_Current
        End If
        'Loop 
    Next     
End Sub

您需要一个 ID 为 "ID"

的控件
<ItemTemplate>
    <asp:HiddenField ID="ID" runat="server" Value='<%# FillMeSomehow%>' />
    <asp:CheckBox ID="cbSelect" runat="server" AutoPostBack="false" />
</ItemTemplate>

使用 FindControl 你会发现控件(令人惊讶)而不是字符串。而且您必须传递控件的 ID,而不是像 Id.

这样的 属性

所以这没有多大意义:

Dim ID_Current As String = row.FindControl("ID").ToString()

相反,您可以将 ID 存储在 HiddenField 中,因此在 aspx 中:

<asp:HiddenField ID="HiddenID" runat="server" Value='<%# Eval("ID_Current") %>' />

现在您可以通过 FindControl:

获取对 HiddenField 的引用
Dim hiddenID As HiddenField = DirectCast(row.FindControl("HiddenID"), HiddenField)
Dim ID_Current As String = hiddenID.Value