Selected GridView row's Primary key => Session variable RRS feed

  • Question

  • User1864322503 posted

    Hi all,

    I have a searchable GridView, with a Link Button -

    <asp:LinkButton id="lbSelect" runat="server" CommandName="Select" OnClick="lbSelect_Click" />

    for each row of the results of the search. What I would like to implement next is being able to click 'Select' and get my Primary key for that specific row, and then store it in a session var for a SQL Query on a later page. The Primary key column name is EMPID (hidden from GridView). Any help is appreciated.

    A resource of this being done clearly is also helpful, thanks =)

    Tuesday, August 13, 2013 3:09 PM

All replies

  • User-1716253493 posted

    Set GridView DataKeyNames

    <asp:GridView ID="GridView1" runat="server" DataKeyNames="EMPID" ... >

    Use button with CommandName="select" (not click), or use CommandField

    <asp:LinkButton id="lbSelect" runat="server" CommandName="Select" />

    Save the value in Gridview selectedindexchanged event

    Protected Sub GridView1_SelectedIndexChanged(ByVal sender As Object, ByVal e As System.EventArgs) 
    Handles GridView1.SelectedIndexChanged
            Session("EMPID") = GridView1.SelectedValue.ToString()
    End Sub

    Tuesday, August 13, 2013 10:00 PM
  • User1864322503 posted

    Hi oned_gk,

    how could one do this in C# rather than VB if you don't mind.


    Wednesday, August 14, 2013 9:09 AM
  • User-1716253493 posted
    Double click the GridView, type : Session["EMPID"] = GridView1.SelectedValue.ToString();
    Wednesday, August 14, 2013 10:00 AM