ASP.Net Button Command Event

Updated on 26 Jun 2012,
Published on 25 Sep 2009

The ASP.Net Button Command event is used when there is a command name associated with it. This event of Button control also fires when it is being clicked by the user. You can use the Command event of Button control for multiple buttons placed on the ASP.Net web page. Each button control will have its own Command Name associated with it that will direct the command event handler to execute the server code based on the name of command for example edit, delete, sort etc. The ASP.Net Button control has OnCommand method that can be used to trap the click and perform the action at server side according to the value of CommandName property associated with the clicked button. You can handle this event without attaching any delegate.

The Command event handler of Button control uses CommandEventArgs class object as parameter that provides the access to the two properties of Button Control:

1. CommandName

2. CommandArgument

You can specify the values in the HTML markup code block of ASP.Net button control. Both CommandName and CommandArgument property accept string type value that can also be specified programmatically.

Button Control Examples:

You can see the live samples and examples of Button Control from the following links:

Attaching Command Event with Button Control

There are 2 ways for attaching the OnCommand event handler with ASP.Net Button control:

1. Click to select the Button control in the Design view of web page in Visual Web Developer. Then press F4 to open its property fly-out window. Change the property window’s view to events as shows in the figure below:

Double click on the Command method to generate the command event handler code for the selected Button control.

<asp:Button ID="Button1" 
    runat="server" 
    Text="Button 1" 
    OnCommand="Button1_Command"/>

It will generate the following code for the Button Control:

protected void Button1_Command(object sender, CommandEventArgs e)
{

}

2. You can also attach the event programmatically using server code at Page_Load event of web page as follow:

protected void Page_Load(object sender, EventArgs e)
{
    Button1.Command += new CommandEventHandler(Button1_Command);
}

protected void Button1_Command(object sender, CommandEventArgs e)
{
    throw new Exception("The method or operation is not implemented.");
}

For attaching the event programmatically write the ID of the button control inside the Page_Load event, as "Button1" used in above sample code. Next use the dot operator to open the intellisense showing properties and methods available for it and select the Command event from the list. Then add "+=" and press tab to auto generate the handler code for it. You will get the code similar to the sample code shown above.

Example for ASP.Net Button Command

HTML Code
<asp:Button ID="Button1" 
    runat="server" 
    CommandArgument="1" 
    CommandName="Insert" 
    Text="Button 1" 
    OnCommand="Button1_Command"/>
C# Code
protected void Button1_Command(object sender, CommandEventArgs e)
{
    Response.Write("Command Name = <b>" + e.CommandName + "</b>");
    Response.Write("<br />Command Argument: <b>" + e.CommandArgument + "</b>");
}

Output:

Get the output for above discussed code from the following link:

Button Command Event

Continue to next tutorial: ASP.Net Button OnClientClick Property to learn how to use the client end script with Button control that will execute before sending web page data to the server.

0 Responses to "ASP.Net Button Command Event"
Leave a Comment
* required
* required
* will not be published
* optional
* hint: http://www.example.com
  • Subscribe via Email