如何从 ASP.NET Form DataGrid 中检索选定的行

How to retrieve the selected rows from ASP.NET Form DataGrid

我正在为在 ASP.Net Forms/VB.Net 和 .Net 3.5 中开发的旧项目开发新功能,其中一个功能是批处理操作,它包括让用户 select 来自 DataGrid(假设它是 GridA)的多行 他按下按钮开始操作,服务器应该在另一个网格(假设它的 GridB)中显示 selected 项目列表弹出窗口(我正在使用 AjaxToolkit 中的 ModalPopupExtender)用户应该在弹出窗口中填写一些信息,然后使用 'Validate' 按钮验证操作。

因此,为了实现 selection 行,我添加了一个显示复选框的 TemplateColumn,如下所示:

<asp:TemplateColumn>
  <HeaderStyle HorizontalAlign="Center"></HeaderStyle>
  <ItemStyle HorizontalAlign="Center"></ItemStyle>
  <HeaderTemplate>
    <asp:CheckBox ID="SelectAll" runat="server" />
  </HeaderTemplate>
  <ItemTemplate>
    <asp:CheckBox ID="ItemSelector" runat="server" />
  </ItemTemplate>
</asp:TemplateColumn>

DataGrid (GridA) 绑定到直接从数据库检索的数据集。

我的问题是如何在用户按下启动操作的按钮 'Start' 后从 GridA 中获取 selected 项目的列表(在服务器端)?

如果您对如何实现此方案有其他建议,欢迎提出您的想法:-)

我终于弄明白了,

在操作按钮单击处理程序上,我添加了这段代码,允许我识别所选行并获取数据项的 ID:

For Each item As DataGridItem In objDataGrid.Items
    Dim checkBox As CheckBox = CType(item.FindControl("ItemSelector"), CheckBox)

    If Not checkBox Is Nothing AndAlso checkBox.Checked Then
       ' My row ID is in the Third cell in an invisible column
       Dim cell As TableCell = item.Cells(2)
       If Not cell Is Nothing AndAlso cell.Text <> vbNullString Then
          ' Registering the row ID on an ArrayList to retrieve it later
          SelectedCards.Add(cell.Text)
       End If
    End If
Next

之后,我使用存储在 SelectedCards ArrayList 中的 ID 从数据库加载我选择的数据项。

祝你好运!