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:
Copy codeThe Code is as follows: <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:Copy codeThe Code is 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"Copy codeThe Code is as follows: <asp: Button ID = "Button1"
Runat = "server" Text = "Button"
Onclick = "button#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.Copy codeThe Code is as follows: 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: Copy codeThe 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:Copy codeThe Code is as follows: protected void Page_Load (object sender, EventArgs e)
{
This. Form. DefaultButton = "ContentPlaceHolder1 $ btsubmit ";
}
Or on the Content PageCopy codeThe Code is as follows: protected void Page_Load (object sender, EventArgs e)
{
This. Page. Form. DefaultButton = "ContentPlaceHolder1 $ btsubmit ";
}
(2) in the category of non-Parent Board pages:Copy codeThe Code is as follows: 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.