Link to home
Start Free TrialLog in
Avatar of gfedz
gfedzFlag 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

ASKER CERTIFIED SOLUTION
Avatar of Jini Jose
Jini Jose
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
also change your rowcommand to RowDeleting(object sender, GridViewDeleteEventArgs e)
Avatar of 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
Avatar of codingbeaver
codingbeaver
Flag of United States of America 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
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.
Even simpler, just change this line:

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

To this:

Dim bonusID As Integer = e.CommandArgument
Avatar of 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
Does my second suggestion work?
Avatar of gfedz

ASKER

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