【问题标题】:How to retrieve the selected rows from ASP.NET Form DataGrid如何从 ASP.NET Form DataGrid 中检索选定的行
【发布时间】:2015-07-19 07:06:11
【问题描述】:

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

所以,为了实现行选择,我添加了一个 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) 绑定到直接从数据库中检索的数据集。

我的问题是如何在用户按下开始操作的“开始”按钮后从 GridA 获取所选项目的列表(在服务器端)?

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

【问题讨论】:

    标签: asp.net vb.net datagrid webforms


    【解决方案1】:

    我终于明白了,

    在操作按钮单击处理程序上,我添加了这段代码,允许我识别选定的行并获取数据项的 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 从数据库中加载我选择的数据项。

    祝你好运!

    【讨论】:

      猜你喜欢
      • 2011-07-11
      • 2012-11-05
      • 2023-04-07
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-05-30
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多