How to access "command name" which has been defined in Footer Template of Gridview?

2.3k views Asked by At

So I have a gridview where inside the FooterTemplate I have defineted a commandname for buttton Add. Here the code for that:

                                      <asp:TemplateField>
                                    <HeaderStyle Font-Size="XX-Small" Font-Names="Verdana" Font-Bold="True" VerticalAlign="Top"></HeaderStyle>
                                    <FooterTemplate><asp:LinkButton CommandName="Insert" Text="Add credit" ID="btnAdd" OnClick="btnAdd_Click" Runat="server" Font-Size="XX-Small  "
                                            Font-Names="Verdana" Font-Bold="True" ForeColor="#5BB1E6" Width="7%"></asp:LinkButton>
                                    </FooterTemplate>
                                </asp:TemplateField>

Just like this Add button I have another button called Edit and I want to put SQL statements for all these buttons in one function. I wanted to do something like this:

protected bool AddNewCredit()

        if (e.Commandname == "Insert")
    {
    Do This}
    if (e.Commandename == "Edit")
    {
    Do this
    } 

And so on. Problem is when I try to access e.rowcommand it doesn't catch commandname after e.

Help is appreciated to acheive my goal which is to be able to read the value of e.commandname in a function. Thanks a lot everyone :)

1

There are 1 answers

1
Lawrence Johnson On

Change it to this:

<asp:LinkButton CommandName="Insert" Text="Add credit" ID="btnAdd" OnCommand="btnAdd_Click" Runat="server" Font-Size="XX-Small  "
                                        Font-Names="Verdana" Font-Bold="True" ForeColor="#5BB1E6" Width="7%"></asp:LinkButton>

then change your btnAdd_Click function to this:

public void btnAdd_Click(object sender, CommandEventArgs e)
{
    string sCommandName = e.CommandName;
}

If you want to catch CommandName and CommandArgument you need to use the OnCommand event, not OnClick.