Asp.net page press enter to trigger the button event
In depth, this is not an ASP. NET issue, but how the submit button of the html form is designed.
When the focus of your cursor enters a form element, the first button in the form (flow layout follows left to right, top to bottom) type = submit (if any) will be activated ), 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, no additional script is required to submit the form. The submit button is designed to submit the form.
In 1.x, it is displayed as <input type = button onclick = _ doPostBack (...)/> this normal button does not have the above default behavior of submit.
3. disable this default action. Method 2:
1. Set the defualtButton of the form element as follows:
<Form id = "form1" runat = "server" defaultbutton = "Button1">
Pay attention to defaultButton = <TargetButton. ID>. Therefore, the buttons in the composite control such as the template may be invalid (not tested)
2. Modify the button Rendering Method UseSubmitBehavior = "false"
<Asp: Button ID = "Button1" runat = "server" Text = "Button" onclick = "button#click" UseSubmitBehavior = "false"/>
(Csdn Xiaofeng residual month)
In addition, you can filter the carriage return practices by controlling the focus. You need to record it to obtain the ID of the control where the current page focus is located:
Document. activeElement
For Asp. Net. We enter the content in TextBox1 and press enter to execute the click method of button1. Write it 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 };");