5

I am trying to pass a CustomerID value to codebehind, from my LinkButton in my gridview control. I tried the solution suggested here but it does not work.

My gridview code is:

<asp:TemplateField HeaderText="Last Name, First Name">
    <ItemTemplate>
        <asp:LinkButton OnClick="EditCustomer" id="lbtnCustomerName" CommandName="CustomerName" Visible="true" runat="server" ToolTip="Click to edit customer."><%# DataBinder.Eval(Container.DataItem, "custLastName") + ", " + DataBinder.Eval(Container.DataItem, "custFirstName" + ", " + DataBinder.Eval(Container.DataItem, "custID")%></asp:LinkButton>
    </ItemTemplate>
</asp:TemplateField>


protected void EditCustomer(Object sender, EventArgs e)
{

}

How can I get the custID value in the EditCustomer event?

Community
  • 1
  • 1
DNR
  • 3,706
  • 14
  • 56
  • 91

1 Answers1

10

Youc an pass the CustomerID as CommandArgument:

<asp:LinkButton OnClick="EditCustomer" id="lbtnCustomerName" 
     CommandArgument='<%#Eval("CustomerID")%>'
     CommandName="CustomerName"
     OnCommand="LinkButton_Command"
     Visible="true" runat="server"
     ToolTip="Click to edit customer."><%# DataBinder.Eval(Container.DataItem, "custLastName") + ", " + DataBinder.Eval(Container.DataItem, "custFirstName" + ", " + DataBinder.Eval(Container.DataItem, "custID")%>
</asp:LinkButton>

Now you can handle the LinkButton's Command event:

void LinkButton_Command(Object sender, CommandEventArgs e) 
{
   String CustomerID = e.CommandArgument.ToString();
}
Tim Schmelter
  • 450,073
  • 74
  • 686
  • 939
  • Thanks Tim. How would the code behind appear? I am getting an error "No overload for 'EditCustomer' matches delegate 'System.EventHandler' " – DNR Jul 24 '12 at 18:02
  • @DotNetRookie: Edited my answer to show how you handle the LinkButton's Command event. – Tim Schmelter Jul 24 '12 at 18:15