asp.net page return triggers the button button event
Deep down, this is not a question of asp.net, but of how the HTML form's Submit button is designed.
When your cursor focus enters a FORM element, it activates the first (if any) button in the form (the flow layout obeys left to right, top to bottom) Type=submit, waits for a response to the carriage return event, and submits 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. button in asp.net 2.0 is rendered as <input type=submit> this time no additional script submission form, submit button is designed to submit form
And in 1.x it is presented as <input Type=button onclick=_dopostback (...)/> This normal button does not have the default behavior of Submit
3. Disabling this default behavior has Law II
1. Set the form element's Defualtbutton for the real button you want to respond to the carriage return as follows
<form id= "Form1" runat= "Server" defaultbutton= "Button1" >
Note DefaultButton = < <TargetButton.ID>> Therefore this may not be valid for a Button in a composite control such as a template (not tested)
2. Modify button Rendering mode usesubmitbehavior= "false"
<asp:button id= "Button1" runat= "Server" text= "button" onclick= "Button1_Click" usesubmitbehavior= "false"/>
(CSDN month)
In addition, by controlling the focus of the way, filter return practice, you need to record a note, get the current page focus of the control's ID:
Document.activeelement
For asp.net. After we enter the content in the TextBox1, press the ENTER key to execute the Button1 click Method. Then write 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}; ");