Differences between OnClick and OnCommand

Source: Internet
Author: User

<Asp: button id = "button1" Runar = "server" onclick = "button#click" oncommand = "button#command">

Both onclick and oncommand are defined,
Define related events in code

Button#click (Object sender, eventargs e ){}

Buttonstmcommand (Object sender, commandeventargs e ){}

1. click the button to trigger the two events at the same time, But click the button first and then Run Command

2. Differences

Command can distinguish different buttons by setting commandname and commandargument,
You can use commandeventargs that contains event data to obtain or set

In click, you can also obtain commandname and commandargument in the form of (button) sender.

Example: onclick
<Asp: button id = "button1" Runar = "server" onclick = "button#click" commandname = "A" text = "A">
<Asp: button id = "button2" Runar = "server" onclick = "button#click" commandname = "B" text = "B">
<Asp: button id = "button3" Runar = "server" onclick = "button#click" commandname = "C" text = "C">
Defining a button#click (object sender, EventArgs e) {} is enough,
Other buttons need to be used (Button) sender. CommandName to determine which Button is used.

The OnCommand is the same, except for e. CommandName.

(Event registration can also be omitted in ASP. NET2.0)

3. Both methods are server-side events. To add client events, you must use Attributes to set the onclick client events for the Button, for example:
Button1.Attributes. Add ("onclick", "yourfunction ();")
You can also define the yourfunction () method on the aspx page.

4,About TextBox1.Attributes. Add

--- Question ---------
My page has two buttons Button1, Button2, and two edit boxes TextBox1 and TextBox2 respectively. After I add the following line of code in PAGE_LOAD,

TextBox1.Attributes. Add ("onkeydown", "if (event. keyCode = 13) {document. all. Button1.click ();}");

When editing textbox1, you can press ender to directly execute the button1.click code.
When textbox2 is edited, the code of button1.click is directly executed when you press ender.

So I added

Textbox2.attributes. Add ("onkeydown", "If (event. keycode = 13) {document. All. button2.click ();}");

However, when you edit textbox2, you can still directly execute the code of button1.click without executing the code of button2.click.

Now, even if I delete the two statements and edit textbox1 and textbox2, click ender to directly execute the code of button1.click.

I would like to ask what the reason is and how to solve and avoid this situation.

--- Cause of this situation ------------
This is determined by the features of web forms. button1 is the default form submission button. As long as it is in this form, the carriage return function is triggered on all controls except the multi-line text box.

. So this will happen !!!

 

--- Solution to the compile method ---------------
Add
Me.txt ApplyID. attributes. add ("onkeydown", "if (event. keyCode = 13) {document. all. "+ butQuery. clientID + ". click (); return false }")
Me.txt CHANGEID. attributes. add ("onkeydown", "if (event. keyCode = 13) {document. all. "+ butQuery. clientID + ". click (); return false }")

TxtApplyID is the incoming frame. When the incoming frame is finished, press the bicycle, and then click the button next to the row.

 

 

Reproduced http://www.cnblogs.com/xy6521/articles/1352474.html

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.