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
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
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Is the SelectedIndexChanged firing or not??
if no, try deleting the event handler and add a new event handler and check it out..
if no, try deleting the event handler and add a new event handler and check it out..
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.
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
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.
ASKER
Thanks! :-)
ASKER
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_resu
enableEventValidation="fal
Also, I have set AutoGenerateSelectButton="
Finally, I use this code to set the DataKeyNames property and populate the gridview.
If dsInsp.Tables(0).Rows.Coun
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