Jagadisha B S
Jagadisha B S

Reputation: 709

How to find Grid view button click event?

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

Upvotes: 3

Views: 13697

Answers (3)

P. Bhanu Tej
P. Bhanu Tej

Reputation: 1

     <!--We use onrowcommand for getting the selected row -->
<asp:GridView runat="server" ID="gvTest"  AutoGenerateColumns="False"  OnRowCommand="gvTest_OnRowCommand" >
             <Columns>
                <asp:TemplateField HeaderText="BookId" >
               <ItemTemplate>
                   <asp:Label runat="server" ID="lblBookId" Text='<%# Bind("[BookId]") %>'></asp:Label>
               </ItemTemplate>
                    <EditItemTemplate>
                        <asp:TextBox runat="server" ID="txtBookId" Text='<%# Bind("[BookId]") %>'></asp:TextBox>
                    </EditItemTemplate>
                </asp:TemplateField>
             </Columns>
            </asp:TemplateField>
                <asp:TemplateField HeaderText="Options">
                    <ItemTemplate>
                      <asp:Button runat="server" ID="btnDelete"  CommandArgument="<%# Container.DisplayIndex %>" CommandName="IsDelete" Text="Delete"></asp:Button>
                    </ItemTemplate>
                </asp:TemplateField>

 //Code Behind

 protected void gvTest_OnRowCommand(object sender, GridViewCommandEventArgs e)
    {
        try
        {
            //getting rowindex which we have selected by using CommandArgument

                int rowindex = Convert.ToInt32(e.CommandArgument);


                    if (e.CommandName == "IsDelete")
                    {

                        int bkid = gvTest.Rows[rowindex].Cells[0].FindControl("BookId");

                        //call a method to delete book using the bkid 

                    }

        }
        catch (Exception ex)
        {

            Response.Write(ex);
        }
    }

Upvotes: 0

Karl Anderson
Karl Anderson

Reputation: 34844

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"  
        OnRowCommand="GridView1_RowCommand">
    <Columns>
        <asp:TemplateField HeaderText="Add Record">
            <ItemTemplate>
                <asp:Button ID="btnAddRecord"  
                            CommandArgument="<%# ((GridViewRow)Container).RowIndex %>" 
                            CommandName="AddRecord" runat="server" Text="Add" />
            </ItemTemplate>
        </asp:TemplateField> 
    </Columns>
</asp:GridView>

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
    }
}

Upvotes: 6

Sasidharan
Sasidharan

Reputation: 3750

1.Give a command name for button..
2.Then inside gridview

if e.commandname="yourcommandname"
{
//your code..
}

Upvotes: 0

Related Questions