There is no essential difference between the two events, which is to click to execute an event. The difference is that the parameters passed are different, the OnClick (object sender, EventArgs e), OnCommand (object sender, CommandEventArgs e), and then the value in the event is slightly different.
Another benefit of using OnCommand is that you can specify the same method for multiple buttons, and then distinguish which button is triggered based on different commanname. When a button has two methods at the same time, they are executed in order to execute the click First, then execute the command
<asp:button id= "Button1" runar= "Server" onclick= "Button1_Click" oncommand= "Button1_command" >
Both the onclick and the OnCommand are defined,
Defining related events in code
Button1_Click (Object Sender,eventargs e) {}
Button1_command (Object Sender,commandeventargs e) {}
1. Clicking the button triggers both events, but first executes the click and then executes the command
2. Different places
command can distinguish different buttons by setting CommandName and CommandArgument.
The CommandEventArgs that contains the event data can be used to get or set
and click, you can also get CommandName and CommandArgument, via (Button) sender in this form.
Example: OnClick
<asp:button id= "Button1" runar= "Server" onclick= "Button1_Click" Commandname= "a" text= "a" >
<asp:button id= "Button2" runar= "Server" onclick= "Button1_Click" commandname= "B" text= "B" >
<asp:button id= "Button3" runar= "Server" onclick= "Button1_Click" Commandname= "C" text= "C" >
Defining a Button1_Click (Object Sender,eventargs e) {} is enough,
Other need to use (Button) Sender.commandname to determine which Button.
Instead of OnCommand, the e.commandname can be used to determine the difference.
(in asp.net2.0, event registration can also be omitted)
3, both of these methods are server-side events, if you need to increase the client event, you need to use attributes to onclick the button to the client event settings, such as:
BUTTON1.ATTRIBUTES.ADD ("onclick", "yourfunction ();")
You can also define the YourFunction () method in the ASPX page.
Original author Link--http://www.cnblogs.com/zxjyuan/archive/2010/04/29/1723670.html
"Reprint" The difference between the onclick and OnCommand