Search This Blog

Tuesday, August 31, 2010

Get CommandArgument on Click Event of Button in TemplateField

GridView Markups

<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns = "false">
    <Columns>
        <asp:TemplateField>
        <ItemTemplate>
           <asp:Button ID="Button1" runat="server" Text='<%#Eval("CustomerID")%>'
                  CommandArgument = "Button1"  OnClick = "Button1_Click" />
        </ItemTemplate>
        </asp:TemplateField>
    </Columns>
</asp:GridView>
Many occasions you will need the properties of the Button like its CommandName, CommandArgument. So in that case you will need to get the reference of the Button using the sender parameter.  
Code Behind
protected void Button1_Click(object sender, EventArgs e)
{
    Button btn = (Button)sender;
    string CommandName = btn.CommandName;
    string CommandArgument = btn.CommandArgument;
}

No comments:

Post a Comment

Note: Only a member of this blog may post a comment.