Link to home
Start Free TrialLog in
Avatar of llj45
llj45

asked on

gridview find selected id/key of selected row

Hi,
How to find the value of the id / primary key column of a given row in a gridview?  I wish to put the value into a session variable to be passed to another page.  This value is located in the gridview in the boudfield named num.

The data for this gridview is supplied from other code behind.  This code is not shown below.  The data already displays in the gridview with no problems.

I have supplied the code that I am using.
Thanks, LLJ45
aspx code:
<asp:GridView ID="grdResults" runat="server" AutoGenerateColumns="false"
                            HorizontalAlign="Center" BorderWidth="5" CellPadding="4" >
                            <Columns>
                                <asp:BoundField DataField="Name" HeaderText="Name" />
                                <asp:BoundField DataField="Certification Date" HeaderText="Certification Date" />
                                <asp:BoundField DataField="Expiration Date" HeaderText="Expiration Date" />
                                <asp:TemplateField HeaderText="Select For Credit">
                                    <ItemTemplate>
                                        <asp:Button ID="btnSelect" Runat="server" Text="" CommandName="Select">
                                        </asp:Button>
                                    </ItemTemplate>
                                </asp:TemplateField>
                                <asp:BoundField DataField="num" visible="false" />
                            </Columns>
                        </asp:GridView>

aspx.vb code:
Protected Sub grdResults_RowCommand(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewCommandEventArgs) Handles grdResults.RowCommand
        If e.CommandName = "Select" Then
            Response.Redirect("~/credit_test_results2.aspx", True)
        End If
End Sub

Protected Sub grdResults_RowDataBound(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewRowEventArgs) Handles grdResults.RowDataBound

        If e.Row.RowType <> DataControlRowType.Header And e.Row.RowType <> DataControlRowType.Footer Then
            Dim btn As Button = CType(e.Row.Cells(0).FindControl("btnSelect"), Button)
            btn.Text = "Select For Credit " & Web.UI.DataBinder.Eval(e.Row.DataItem, "Name")
            btn.OnClientClick = "javascript:return confirm('Click OK, To Credit\n\n" & Web.UI.DataBinder.Eval(e.Row.DataItem, "Name") & " \n\nOne Test!');"

End Sub

Protected Sub grdResults_SelectedIndexChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles grdResults.SelectedIndexChanged
' this event never seems to fire?
End Sub

Open in new window

ASKER CERTIFIED SOLUTION
Avatar of rajeeshmca
rajeeshmca
Flag of India image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of llj45
llj45

ASKER

Hi, rajeeshmca
  Thanks for the help!
  I continue to find that the SelectedIndexChanged event of the gridview is NOT firing.  I have done some research.  I have this code at the top of my aspx page.  Also, I have set a breakpoint in the code behind on the SelectedIndexChanged event.

<%@ Page EnableViewState="true" Language="VB" AutoEventWireup="true" CodeFile="credit_test_results.aspx.vb" Inherits="esc_inspector_credit_test_results"
    enableEventValidation="false" %>

Also, I have set AutoGenerateSelectButton="true" on the gridview to create the Select button which should fire the SelectedIndexChanged event when the Select button is clicked?

Finally, I use this code to set the DataKeyNames property and populate the gridview.
If dsInsp.Tables(0).Rows.Count > 0 Then
                    grdResults.DataKeyNames = New String() {"num"}
                    grdResults.DataSource = dsInsp.Tables(0)
                    grdResults.DataBind()
End If
where num is the "primary key field" for the data in the gridview
OK?

LLJ45
Is the SelectedIndexChanged firing or not??
 if no, try deleting the event handler and add a new event handler and check it out..
Avatar of llj45

ASKER

Hi, rajeeshmca
  Thanks for the help!
  Your code worked and answered my question.
Thanks, Lloyd

PS
In .NET 3.5 don't ever use a button like this one:
<asp:Button ID="btnSelect" Runat="server" Text="" CommandName="Select"></asp:Button>
on a gridview with a template field and with the gridview's own select button.
  The CommandName ="Select" will cause the SelectedIndexChanged event not to fire on the gridview!  If this command name is changed to something else, then the SelectedIndexChanged event fires.  Obivously, the command name of the <asp:Button> conflicts with the gridview's "select" button's code.
Avatar of llj45

ASKER

Thanks!  :-)