The ASP. NET carriage return submission event is not a programming problem of ASP. NET, but a detailed discussion about how to plan the submit button in HTML form. It can also be attributed to a part of ASP. NET programming. What is the specific implementation of ASP. NET carriage return event submission? Let's take a look at the following:
ASP. NET press ENTER submit event Implementation 1,
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﹥
ASP. NET press ENTER submit event implementation 2.
In ASP. in NET 2.0, the button is rendered as <input type = submit> by default. At this time, no additional script is required to submit the form, the submit button is designed to be used to submit a 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
ASP. NET press ENTER submit event Implementation 3. disable this default behavior with 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="Button1_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 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}; ");
When the Form runat = server is used in ASP. NET, the <asp: button... in the form cannot be submitted by carriage return.
Now I finally found a property to do this. Use this. Form. defaultbutton = "contentplaceholder1 $ btsubmit ";
It is worth noting that if masterpage is used, the ID of the motherboard MUST be added before the button ID: contentplaceholderid and a dollar sign ($)
C # The implementation code is as follows:
- <%@ Page Language = "C #"
- Masterpagefile = "~ /Masterpage. Master"
- Autoeventwireup = "true"
- Codefile = "login. aspx. cs"
- Inherits = "login" %>
- <Asp: Content ID = "content1"
- Contentplaceholderid = "contentplaceholder1"
- Runat = "server">
- <Asp: textbox runat = "server"
- Id = "WD"> </ASP: textbox>
- <Asp: button id = "btsubmit"
- Runat = "server" text = "Submit"
- Onclick = "btsubmit_click"/>
- ...........................
- </ASP: content>
(1) category with parent board pages:
- protected void Page_Load(object sender, EventArgs e)
- {
- this.Form.DefaultButton = "ContentPlaceHolder1$btsubmit";
- }
Or on the Content Page
- protected void Page_Load(object sender, EventArgs e)
- {
- this.Page.Form.DefaultButton = "ContentPlaceHolder1$btsubmit";
- }
(2) in the category of non-Parent Board pages:
- protected void Page_Load(object sender, EventArgs e)
- {
- this.Form.DefaultButton = "btsubmit";
- }
ASP. NET press enter to submit the event details here, I hope to learn more about ASP. Net Press enter to submit the event.