Avatar of gfedz
gfedz
Flag for United States of America asked on

GridView TemplateField LinkButton Click returning wrong ID

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>

Open in new window

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

Open in new window

ASP.NET

Avatar of undefined
Last Comment
gfedz

8/22/2022 - Mon
ASKER CERTIFIED SOLUTION
Jini Jose

Log in or sign up to see answer
Become an EE member today7-DAY FREE TRIAL
Members can start a 7-Day Free trial then enjoy unlimited access to the platform
Sign up - Free for 7 days
or
Learn why we charge membership fees
We get it - no one likes a content blocker. Take one extra minute and find out why we block content.
Not exactly the question you had in mind?
Sign up for an EE membership and get your own personalized solution. With an EE membership, you can ask unlimited troubleshooting, research, or opinion questions.
ask a question
Jini Jose

also change your rowcommand to RowDeleting(object sender, GridViewDeleteEventArgs e)
gfedz

ASKER
That does work and I did know about that.  I would like to keep it all in the RowCommand.  I have done this before and it has worked with the code I put up.  I'm curious on why it is not working now.  There has to be a way to make this work in the RowCommand.
SOLUTION
codingbeaver

Log in or sign up to see answer
Become an EE member today7-DAY FREE TRIAL
Members can start a 7-Day Free trial then enjoy unlimited access to the platform
Sign up - Free for 7 days
or
Learn why we charge membership fees
We get it - no one likes a content blocker. Take one extra minute and find out why we block content.
Not exactly the question you had in mind?
Sign up for an EE membership and get your own personalized solution. With an EE membership, you can ask unlimited troubleshooting, research, or opinion questions.
ask a question
Jini Jose

RowCommand will also work. But I didnt run your code. for deleting the rows, gridview itself has an event RowDeleting , so it is best practice to use the RowDeleting.
Experts Exchange has (a) saved my job multiple times, (b) saved me hours, days, and even weeks of work, and often (c) makes me look like a superhero! This place is MAGIC!
Walt Forbes
codingbeaver

Even simpler, just change this line:

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

To this:

Dim bonusID As Integer = e.CommandArgument
gfedz

ASKER
gmailjini solution worked but not what I was looking for, I do agree that it is best practice though.

codingbeaver gave me helpful information that pointed me in the right direction.

I did a little trial and error and came up with this that works:

Dim row As GridViewRow = CType(CType(e.CommandSource, Control).NamingContainer, GridViewRow)

Dim bonusID As Integer = myGVSRB.DataKeys(row.RowIndex).Value
codingbeaver

Does my second suggestion work?
Get an unlimited membership to EE for less than $4 a week.
Unlimited question asking, solutions, articles and more.
gfedz

ASKER
Typically that is what I would do to accomplish what I need.  For this specific piece it did not work.