Archana B.R
Archana B.R

Reputation: 407

How to trigger a function when LinkButton in each row of the Grid is clicked

I have a GridView in which I have put an extra column Details, and in each row of the GridView, I have a LinkButton called Details. So when I click this I want an event to trigger.

The asp.net code is:

<asp:TemplateField HeaderText="Details">
<ItemTemplate>
<asp:LinkButton ID="Details" runat="server" Text="Details"></asp:LinkButton>
</ItemTemplate>
</asp:TemplateField>

Kindly help. Thank you.

Upvotes: 0

Views: 1065

Answers (3)

ravisilva
ravisilva

Reputation: 259

You can have a command argument to determine which row was clicked (assuming you have more than one button (standard buttons/link buttons) in a row

<asp:GridView OnRowCommand="GridViewOnItemCommand" runat="server">
  <asp:TemplateField HeaderText="Details">
    <ItemTemplate>
      <asp:LinkButton ID="btnDetails" CommandName="Details" CommandArgument="YOUR_COMMAND_ARG_HERE" Text="Details" runat="server"/>
      <asp:LinkButton ID="btnDelete" CommandName="Delete" CommandArgument="YOUR_COMMAND_ARG_HERE" Text="Delete" runat="server"/>
    <ItemTemplate>
  </asp:TemplateField>
</asp:GridView>

in the code behind file

protected void GridViewOnItemCommand(object sender, GridViewCommandEventArgs e)
{
     //you can determine which button was clicked (detail or delete)
     var command = e.CommandName;

     //you can determine which row was clicked
     var arg = e.CommandArgument;

     if(command == "Details")
          ShowDetails(arg);
     if(command == "Delete")
          Delete(arg);
}

hope this helps

Upvotes: 1

Kapil Khandelwal
Kapil Khandelwal

Reputation: 16144

Handle GridView.RowCommand Event

Occurs when a button is clicked in a GridView control.

<asp:LinkButton ID="Details"  commandname="Details" runat="server" Text="Details"></asp:LinkButton>


void GridView1_RowCommand(Object sender, GridViewCommandEventArgs e)
  {
    // If multiple buttons are used in a GridView control, use the
    // CommandName property to determine which button was clicked.
    if(e.CommandName=="Details")
    {
     // Convert the row index stored in the CommandArgument
     // property to an Integer.
     int index = Convert.ToInt32(e.CommandArgument);

    //Your Code

    }

}

Upvotes: 2

Tim Schmelter
Tim Schmelter

Reputation: 460058

Then use the LinkButton.Click-event:

<asp:LinkButton ID="Details" OnClick="LinkClicked" runat="server" Text="Details">

in codebehind:

protected void LinkClicked(Object sender, EventArgs e) 
{
    LinkButton link = (LinkButton)sender;
    GridViewRow row = (GridViewRow)link.NamingContainer; 
    // assuming there's a label with ID=Label1 in another TemplateField:
    Label label1 = (Label)row.FindControl("Label1");
    label1.Text="You clicked the link button";
}

If you need the GridViewRow of the GridView where the user clicked the link, for example to find controls in other templateFields, you can use the NamingContainerproperty.

Upvotes: 3

Related Questions