Asp.Net: Using the OnCommand Event with CommandArgument

When using a button, linkbutton or imagebutton with CommandArguments or CommandName you can use the OnCommand event instead of the OnClick event. Using the OnCommand Event you use less code to extract the CommandArgument and CommandName from the Event comparing to the OnClick event (because you don’t need to cast the control).
Code example

The Aspx:


<asp:ImageButton ImageUrl="~/Includes/Images/delete_icon.gif" runat="server" ID="ibtDeleteClip" OnCommand="ibtDeleteClip_Command" CommandArgument='' />


And the codebehind (C#):



protected void ibtDeleteClip_Command(object sender, CommandEventArgs e)
{
   string commandArg = e.CommandArgument;
}


Leave a Reply

Your email address will not be published. Required fields are marked *