The difference between the "reprint" OnClick and the OnCommand

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.