asp. net Method for canceling Enter response of text input box in page form

  • 2021-07-07 07:01:03
  • OfStack

This article describes the example of asp. net to cancel the page form text input box Enter response method. Share it for your reference, as follows:

A long time ago, when developing asp. net project, I encountered it: press Enter key on a server TextBox control, and the page will be refreshed once. Later, under google1, it was found that this is the "Enter key" function specially set for form processing by asp. net2.0. For enter key of asp. net ajax form, you can check this article "ASP. NET Enter Key Submission Problem Based on Ajax". The preceding two links tell us how to set the enter key default trigger event. Now there is a new requirement like this. When entering, the entry personnel press the enter key without submitting the form (it is reasonable to think about it. If there are many entry boxes in the form, how many times should the page be sent back if 1 accidentally presses the enter key?) Unless you click the server-side submit button directly. Simply put, it is to remove the enter key function of form elements. Here's my implementation:

1. Preliminary analysis and realization:

1. The page inherits a base class BasePage, and the base class inherits from the Page class. The onkeydown script event of a specific server control is registered in the base class


using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
public class BasePage : System.Web.UI.Page
{
 public BasePage()
 {
 }
 protected override void OnInit(EventArgs e)
 {
  base.OnInit(e);
  CancelFormControlEnterKey(this.Page.Form.Controls);
 }
 /// <summary>
 ///  Here we give Form Add a client to the server control in the onkeydown Footsteps event to prevent server controls from being pressed enter Key direct postback 
 /// </summary>
 /// <param name="controls"></param>
 public static void CancelFormControlEnterKey(ControlCollection controls)
 {
  foreach (Control item in controls)
  {
   // Server TextBox
   if (item.GetType() == typeof(System.Web.UI.WebControls.TextBox))
   {
    WebControl webControl = item as WebControl;
    webControl.Attributes.Add("onkeydown", "if(event.which || event.keyCode){if ((event.which == 13) || (event.keyCode == 13)) {return false;}} ");
   }
   //html Control 
   else if (item.GetType() == typeof(System.Web.UI.HtmlControls.HtmlInputText))
   {
    HtmlInputControl htmlControl = item as HtmlInputControl;
    htmlControl.Attributes.Add("onkeydown", "if(event.which || event.keyCode){if ((event.which == 13) || (event.keyCode == 13)) {return false;}} ");
   }
   // User control 
   else if (item is System.Web.UI.UserControl)
   {
    CancelFormControlEnterKey(item.Controls); // Recursive invocation 
   }
  }
 }
}

In this way, the page that wants to cancel the function of "enter key" can only inherit the BasePage class under 1.

2. Handling of user controls: My idea is to continue to deal with runat=server controls inside user controls in the base class, and the test is also passed.

3. Add onkeydown events directly to html controls without runat=server tags in pages and user controls.

Here is the test page and its corresponding class file:

Test. aspx page:


<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="Test.aspx.cs" Inherits="Test" %>
<%@ Register src="TestUserControl.ascx" tagname="TestUserControl" tagprefix="uc1" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
 <title></title>
</head>
<body>
 <form id="form1" runat="server" >
<input type=text id="txtTest" runat="server" /> <input id="txtTest1" type="text" name="txtTest1" onkeydown="if(event.which || event.keyCode){if ((event.which == 13) || (event.keyCode == 13)) {return false;}}" /> 
<asp:textbox ID="Textbox1" runat="server"></asp:textbox>
 <uc1:TestUserControl ID="TestUserControl1" runat="server" />
<asp:Button ID="btnSubmit" runat="server" Text="Submit" />
 </form>
</body>
</html>

Class:


using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
public partial class Test : BasePage
{
 protected void Page_Load(object sender, EventArgs e)
 {
  Response.Write("123");
 }
}

Then there is a user control:


<%@ Control Language="C#" AutoEventWireup="true" CodeBehind="TestUserControl.ascx.cs" Inherits="MyWeb.TestUserControl" %>
<asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>
<br />
<input id="Text1" type="text" runat="server"/>
<br />
<input id="txtInput" type="text" name="txtInput" onkeydown="if(event.which || event.keyCode){if ((event.which == 13) || (event.keyCode == 13)) {return false;}}" />

On the author's machine, the enter key is pressed according to the above idea for TextBox, HtmlInputText, html control without runat=server tag, and the user control composed of the three.

2. Problems with script improvements

Then I see if (event. which event. keyCode) {if (event. which = = 13) (event. keyCode = = 13)) {return false;}} This sentence keeps appearing, so I kindly encapsulate it in the page as an JavaScript function called forbidInputKeyDown (ev):


<script type="text/javascript">
 function forbidInputKeyDown(ev) {
  if (typeof (ev) != "undefined") {
   if (ev.keyCode || ev.which) {
    if (ev.keyCode == 13 || ev.which == 13) { return false; }
   }
  }
 }
</script>

Then the onkeydown method corresponds to the event "forbidInputKeyDown (event)" (for example, the TextBox control on the server side of the page is rewritten to webControl. Attributes. Add ("onkeydown", "forbidInputKeyDown (event)" when registering the client event);) Strangely, this time, the page was posted back? ! Then the script is debugged and the forbidInputKeyDown function is executed, but form is committed.

I looked at the script location again and moved it from head to body, but the problem remained. Then wonder if the script is wrong. No, the script is correct. Is there something wrong with character? Is there a problem? This confidence is really absent. Is it wrong to register the event? Um...

When I kao, it suddenly dawned on me that the registration event should be written as follows: onkeydown= "return forbidInputKeyDown (event)", that is, it is good to add return before forbidInputKeyDown function, or character.

I hope this article is helpful to everyone's asp. net # programming.


Related articles: