ASP.NET enter to submit a brief analysis of the event

  • 2020-05-16 06:49:58
  • OfStack

The ASP.NET carriage return commit event is not really a programming issue for ASP.NET, but a specific discussion of how the submit button in html form is planned. Also included in part 1 of the ASP.NET programming, what is the concrete implementation of the ASP.NET carriage return commit event? Let's take a look at the details below:
ASP.NET carriage return event implementation
When your cursor focuses on a form element, it activates the first button in the form (flow layout follows left to right, top to bottom) type=submit (if any), waits for the return event, and submits the form
You can test the following 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 enter the commit event implementation 2.
In ASP.NET 2.0, button will be rendered as a wok with input type=submit by default. No additional script is required to submit form. The submit button is designed to submit form and in 1.x it will be rendered as a wok with input type=button onclick=_doPostBack(...) The normal button does not have the above default behavior of submit
ASP.NET carriage return commit event implementation 3. There is a way to disable this default behavior 2
(1) set the form element of defualtButton as the actual button you want to respond to enter as follows
 
 � form id="form1" 
runat="server" 
defaultbutton="Button1" �  

Note that defaultButton = hang TargetButton.ID so this may not be valid for Button in composite controls such as templates (not tested)
(2) modify the presentation mode of button, UseSubmitBehavior="false"
 
 � asp:Button ID="Button1" 
runat="server" Text="Button" 
onclick="Button1_Click" 
UseSubmitBehavior="false" / �  

In addition, you can filter the practice of carriage return by controlling the focus. What you need to record is to get the ID of the control where the current page focus is:
document.activeElement
For ASP.NET. after we enter in TextBox1 and press enter, we execute the click method of Button1. So 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 ASP. NET USES the form runat=server form, the inside of the asp:button... It's annoying not to be able to submit a form by hitting enter.
Now I finally found a property that does this, this.Form.DefaultButton = "ContentPlaceHolder1$btsubmit";
Note that if masterPage(motherboard page) is used, the motherboard ID:ContentPlaceHolderID and a dollar character ($) are added before the button ID
C# 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) in the class containing motherboard page:
 
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 non-motherboard page class:
 
protected void Page_Load(object sender, EventArgs e) 
{ 
this.Form.DefaultButton = "btsubmit"; 
} 

The details of the ASP.NET carriage return submission event will be introduced to you here. I hope it will be helpful for you to understand and learn the ASP.NET carriage return submission event.

Related articles: