DNR
DNR

Reputation: 3736

Passing an id value to LinkButton server side event in gridview control

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?

Upvotes: 5

Views: 8506

Answers (1)

Tim Schmelter
Tim Schmelter

Reputation: 460158

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();
}

Upvotes: 10

Related Questions