Analysis of ASP. NET carriage return submission events

Source: Internet
Author: User

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:

 
 
  1. ﹤form action=""﹥   
  2. ﹤input type="text" /﹥   
  3. ﹤input type="submit" value="submit" /﹥   
  4. ﹤/form﹥   
  5. ﹤form action=""﹥   
  6. ﹤input type="text" /﹥   
  7. ﹤input type="button" value="submit" /﹥   
  8. ﹤/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:

 
 
  1. ﹤form id="form1"   
  2.  
  3. runat="server"   
  4.  
  5. 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"

 
 
  1. ﹤asp:Button ID="Button1"   
  2.  
  3. runat="server" Text="Button"   
  4.  
  5. onclick="Button1_Click"   
  6.  
  7. 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:

 
 
  1. 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.

 
 
  1. TextBox1.Attributes.Add("onkeydown",   
  2.  
  3. "if(event.which || event.keyCode){  
  4.  
  5. if ((event.which == 13) || (event.keyCode == 13)) {  
  6.  
  7. document.getElementById('"+  
  8.  
  9. Button1.UniqueID+"').click();return false;}}   
  10.  
  11. 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:

 
 
  1. <%@ Page Language = "C #"
  2. Masterpagefile = "~ /Masterpage. Master"
  3. Autoeventwireup = "true"
  4. Codefile = "login. aspx. cs"
  5. Inherits = "login" %>
  6. <Asp: Content ID = "content1"
  7. Contentplaceholderid = "contentplaceholder1"
  8. Runat = "server">
  9. <Asp: textbox runat = "server"
  10. Id = "WD"> </ASP: textbox>
  11. <Asp: button id = "btsubmit"
  12. Runat = "server" text = "Submit"
  13. Onclick = "btsubmit_click"/>
  14. ...........................
  15. </ASP: content>

(1) category with parent board pages:

 
 
  1. protected void Page_Load(object sender, EventArgs e)  
  2. {  
  3. this.Form.DefaultButton = "ContentPlaceHolder1$btsubmit";  

Or on the Content Page

 
 
  1. protected void Page_Load(object sender, EventArgs e)  
  2. {  
  3. this.Page.Form.DefaultButton = "ContentPlaceHolder1$btsubmit";  

(2) in the category of non-Parent Board pages:

 
 
  1. protected void Page_Load(object sender, EventArgs e)  
  2. {  
  3. 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.

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.