Js triggers the Onclick event of the asp.net Button. _ javascript skills

Source: Internet
Author: User
Because asp.net adopts the event-driven mode, The onclick event of the button is not triggered by pressing the carriage return by default. If you press enter, the form is not submitted. You can see through httpwath that the page form is actually submitted to the action page under the form, but the onclick event is not triggered, if you are interested in the introduction of event-driven in asp.net, it may be helpful to solve the problem of trigger different events by multiple buttons on a single page, this avoids the trouble of requiring multiple forms in asp or controlling them through js scripts.

Asp.net brings convenience, but also brings about a problem. In actual applications, there are not many buttons on a page. After entering the content in the input box, you can press enter to submit the form. Because asp.net adopts the event-driven mode, The onclick event of the button is not triggered by pressing the carriage return by default. If you press enter, the form is not submitted. You can see through httpwath that the page form is actually submitted to the action page under the form, but the onclick event is not triggered.

In the asp.net event-driven mode, to trigger an event by pressing the carriage return, you must use the js script.
In asp.net's aspx page, form code:

The Code is as follows:




However, you can see in the source code of the access page:

The Code is as follows:




By default, press enter in the input box. In fact, the form is submitted to the corresponding page of the form action, and no event is triggered.
Strange: When a web control is not used on An aspx page, press enter in the input box. By default, The onclick event of a button is not triggered; when a web control is used on the page, press enter in the input box. By default, The onclick event of the first button is triggered. [Here refers to the first button control in the page code, including the button control in the web Control]
The following describes how to trigger the onclick event of the button through js.
The default button control, the code in html is as follows:

The Code is as follows:




In fact, The onclick event triggered by clicking this button calls a js script __dopostback (eventTarget, eventArgument)
The button control has a property: UseSubmitBehavior. The default value is true. When you change it to false, you can view the html source code to see the called js Script Function.

The Code is as follows:




Generated js script:

The Code is as follows:


Related Article

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.