ASP. NET page press enter to trigger the button event

Source: Internet
Author: User

1. In depth, this is not a problem with Asp.net, but how the submit button of the HTML form is designed.
When your cursor focus enters a form element, the first (
Stream layout follows left to right, top to bottom)
Type = submit button (if any), wait for the response to the carriage return event, and submit the form
You can test the code.
<Form action = "">
<Input type = "text"/>
<Input type = "Submit" value = "Submit"/>
</Form>

<Form action = "">
<Input type = "text"/>
<Input type = "button" value = "Submit"/>
</Form>

2. In Asp.net 2.0, the button is displayed as <input type = submit> by default.
In this case, do not submit the form with additional scripts. The submit button is designed to submit the form.
But in 1. <Input type = button onclick = _ dopostback ()/>
This normal button does not have the above default behavior of submit.
3. To disable this default action, follow these steps:
1. Set defaultbutton of the form element as follows.
<Form ID = "form1" runat = "server" defaultbutton = "button1">
Pay attention to defaultbutton = <targetbuttonid>. Therefore, the buttons that conform to the control summary, such as the template, may be invalid.
2. Modify the button Rendering Method usesubmitbehavior = "false"
<Asp: button id = "button1" runat = "server" text = "button" onclick = "button#click" usesubmitbehavior = "false"/>
In addition, you can filter the carriage return practices by controlling the focus. You need to record it to obtain the ID of the Child control of the current page focus.
Document. activeelement
For Asp.net, after entering the content in the textbox, press enter to execute the click method of button1. In
In the page_load event method.
Textbox1.attributes. Add ("onkeydown", "If (event. Which | event. keycode) {if
(Event. Which = 13) | (event. keycode = 13 )){
Document. getelementbyid ('"+ button1.uniqueid +"'). Click (); Return
False ;}} else {return true };");

$ (Document ). ready (function ({$ (": Text "). keypress (function () {If (E. keycode = 13) {$ (": Input "). unbind ("click"); $ ("# B "). fuous (); $ ("# B "). click ()}});});
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.