troubleshooting Question

GridView TemplateField LinkButton Click returning wrong ID

Avatar of gfedz
gfedzFlag for United States of America asked on
ASP.NET
9 Comments2 Solutions1310 ViewsLast Modified:
So I have a gridview with a 'Delete' LinkButton in a TemplateField.  I am using the TemplateField so I can use an OnClientClick confirmation popup.  I bind the datakey to the CommandArgument.  When I click on the 'Delete' button it returns a random value from the items in the gridview.  It's a valid ID but for a different random row.  I have tried different variations on getting the key in the codebehind but nothing seems to work.  Here is the code:

 
<asp:GridView ID="myGVSRB" runat="server" AutoGenerateColumns="False" Font-Size="12" BorderWidth="1px"     DataKeyNames="intSRBBonusID" Width="100%" HeaderStyle-HorizontalAlign="Center" GridLines="Both"   BorderColor="#404040" CellSpacing="0" Font-Names="Veranda,arial,sans-serif" 
  EmptyDataText="No Information Found." ShowHeaderWhenEmpty="True" EmptyDataRowStyle-  HorizontalAlign="Center" RowStyle-HorizontalAlign="Center">
  <AlternatingRowStyle BackColor="#b0c4de" />
  <HeaderStyle ForeColor="White" BackColor="SteelBlue" Font-Bold="True" />
  <Columns>
    <asp:TemplateField>
      <ItemTemplate>
        <asp:LinkButton ID="lnkGetDDFile" runat="server" Text="PopUp" Visible='<%# Not IsDBNull(Eval	  	 ("strControlNbr")) %>' CommandName="cmdViewSRB" />
      </ItemTemplate>
    </asp:TemplateField>
    <asp:BoundField HeaderText="Control #" DataField="strControlNbr" />
    <asp:BoundField HeaderText="Bonus Type" DataField="strBonusDesc" />
    <asp:BoundField HeaderText="Payment Due Date" DataField="dtFirstPayment" />
    <asp:BoundField HeaderText="Payment #" DataField="intPmtNbr" />
    <asp:BoundField HeaderText="Amount" DataField="intTotalBonusAmt" DataFormatString="${0}.00" />
    <asp:BoundField HeaderText="Status" DataField="strStatusDesc" />
    <asp:BoundField HeaderText="Date Paid" DataField="dtReceived" />
    <asp:BoundField HeaderText="Transaction #" DataField="intPmtNbr" />
    <asp:BoundField HeaderText="Remarks" DataField="strRemarks" />
    <asp:TemplateField HeaderText="Attachments">
      <ItemTemplate>
        <asp:HyperLink ID="btnAttch" runat="server" ImageUrl="~/Images/40.png" ToolTip='<%# Bind	  ("strFileName") %>' CommandName="cmdGetAttch" Target="_blank" BorderStyle="None" />
      </ItemTemplate>
    </asp:TemplateField>
    <asp:TemplateField>
      <ItemTemplate>
        <asp:LinkButton ID="btnDeleteSRB" runat="server" Text="Delete" CommandName="cmdDelSRB" 		  CommandArgument='<%# Bind("intSRBBonusID") %>' OnClientClick="return confirm('Are you sure you 	  want to delete this SRB? This action will not be able to be undone.');" />
      </ItemTemplate>
    </asp:TemplateField>
  </Columns>
</asp:GridView>
Protected Sub myGVSRB_RowCommand(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewCommandEventArgs) Handles myGVSRB.RowCommand

        If e.CommandName = "cmdDelSRB" Then 'delete selected SRB

            Dim bonusID As Integer = myGVSRB.DataKeys(e.CommandArgument).Value

            sql = "SELECT strFileName FROM tblSRB WHERE intSRBBonusID = " & bonusID

            myDataTable = New DataTable
            myDataTable = getData(sql)

            lblTest.Text = myDataTable.Rows(0)(0) & " | " & bonusID

        End If

    End Sub
Join the community to see this answer!
Join our exclusive community to see this answer & millions of others.
Unlock 2 Answers and 9 Comments.
Join the Community
Learn from the best

Network and collaborate with thousands of CTOs, CISOs, and IT Pros rooting for you and your success.

Andrew Hancock - VMware vExpert
See if this solution works for you by signing up for a 7 day free trial.
Unlock 2 Answers and 9 Comments.
Try for 7 days

”The time we save is the biggest benefit of E-E to our team. What could take multiple guys 2 hours or more each to find is accessed in around 15 minutes on Experts Exchange.

-Mike Kapnisakis, Warner Bros