【问题标题】:Obtaining the id of the row being edited from a GridView从 GridView 获取正在编辑的行的 id
【发布时间】:2014-04-04 17:58:58
【问题描述】:

我的最终目标是让我的程序像这样工作:

  • 按 GridView 上的编辑按钮
  • RowEditing 事件触发器,创建新窗口
  • 带有 DetailsView 的新窗口显示完整信息。

我已经完成了所有工作,但是,DetailsView 仅显示数据库表中第一个条目中的内容,而不是我按下编辑按钮的条目。

所以我的问题是,如何告诉程序正在编辑 GridView 上的哪一行,并获取信息(如键)以在 DetailsView 上创建新的选择语句?

TablePage.aspx(只有相关代码)

<asp:GridView ID="GridView1" runat="server" OnRowEditing="RowEditing"
    AutoGenerateEditButton="True">
    <Columns>
        <asp:BoundField DataField="name" HeaderText="name" SortExpression="name" />
    </Columns>
</asp:GridView>

TablePage.aspx.vb(同样,只有相关代码)

Protected Sub RowEditing(ByVal sender As Object, ByVal e As GridViewEditEventArgs)
    Dim url As String = "/tables/edit.aspx"
    Dim s As String = "window.open('" & url + "', 'popup_window', 'width=300,height=300s,left=100,top=100,resizable=yes');"
    ClientScript.RegisterStartupScript(Me.GetType(), "script", s, True)
End Sub

【问题讨论】:

  • 最好发布任何代码以更快地获得解决方案,您至少可以发布 gridview 标记和 rowEditing 事件吗?
  • 用相关代码更新了我的帖子,希望足以满足您的需求。
  • 是的,现在我明白了,请检查我的答案。

标签: asp.net sql vb.net gridview


【解决方案1】:

您可以使用 RowEditing 函数中的 e.NewEditIndex 来获取在 GridView 中编辑的当前行,从而获得您可以访问该行并从该行获取任何单元格文本。

请试试这个:

Protected Sub RowEditing(ByVal sender As Object, ByVal e As GridViewEditEventArgs)
    'First, get the current RowIndex
    Dim selectedRowIndex As Integer = e.NewEditIndex

    'Then get any information you need from the row, for example the name of the first cell
    Dim selectedItemText As String = GridView1.Rows(selectedRowIndex).Cells(1).Text

    'Now you can store the information in session to use it in other page
    Session("EditParameter") = selectedItemText
    Dim url As String = "/tables/edit.aspx"

    Dim s As String = "window.open('" & url + "', 'popup_window', 'width=300,height=300s,left=100,top=100,resizable=yes');"
    ClientScript.RegisterStartupScript(Me.GetType(), "script", s, True)
End Sub

当然,您可以添加另一个包含记录号的隐藏绑定字段,然后访问它而不是名称。

希望这会有所帮助。

【讨论】:

  • 是的,这似乎可以从编辑的行中获取数据,谢谢!现在我正在研究如何使用它来更改 DetailsView 中显示的数据。
  • 很高兴它对您有用 :) 请将此标记为答案,以便其他人将来从中受益。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-07-18
  • 2014-10-11
  • 2015-11-23
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多