Jagadisha B S Jagadisha B S - 8 months ago 68
ASP.NET (C#) Question

How to find Grid view button click event?

Button btnAddrecord = (Button)sender;
GridViewRow gvr =(GridViewRow)btnAddrecord.NamingContainer;
if (btnAddrecord.CommandName == "onAddMaterial")


Define the button in your grid view markup and assign a CommandName value to the button, like this:

<asp:GridView ID="GridView1" AutoGenerateColumns="False" runat="server"  
        <asp:TemplateField HeaderText="Add Record">
                <asp:Button ID="btnAddRecord"  
                            CommandArgument="<%# ((GridViewRow)Container).RowIndex %>" 
                            CommandName="AddRecord" runat="server" Text="Add" />

Now in your code-behind, you can handle the OnRowCommand event and AddRecord command, like this:

protected void GridView1_RowCommand(object sender, GridViewCommandEventArgs e)
    if (e.CommandName == "AddRecord")
        // Get index of row passed as command argument
        int index = Convert.ToInt32(e.CommandArgument.ToString());

        // Your logic here