【问题标题】:How to get the ID of the record in the GridViewRow in loop?如何在循环中获取GridViewRow中记录的ID?
【发布时间】:2015-06-01 15:20:40
【问题描述】:

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

错误:

对象引用未设置为对象的实例。

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

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

带有我的Gridview 的 Aspx 代码:

<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 存储过程/动作代码

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

【问题讨论】:

    标签: asp.net vb.net gridview


    【解决方案1】:

    使用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
    

    【讨论】:

      【解决方案2】:

      您需要一个 ID 为“ID”的控件

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

      【讨论】:

        猜你喜欢
        • 2015-05-26
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2021-10-20
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多