Blake
Blake

Reputation: 3

Obtaining the id of the row being edited from a GridView

My end goal is for my program to work like this:

I got all that to work, however, the DetailsView only displays the content from the first entry in the database table, instead of the entry which I pressed the edit button on.

So my question is, how can I tell the program which row is being edited on the GridView, and get the information(like the key) to make a new select statement on the DetailsView?

TablePage.aspx(with only the relevant code)

<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(again, only relevant code)

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

Upvotes: 0

Views: 1002

Answers (1)

jack
jack

Reputation: 1113

You can use e.NewEditIndex from RowEditing function to get the current row edited in GridView, obtaining that you can access the row and get any cell text from the row.

Please try this:

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

Of course you can add another hidden bound field that contains the record number and then access it instead of the name.

Hope this helps.

Upvotes: 1

Related Questions