The example in this article describes the ASP.net implementation method that cancels the enter response of the text input box in the page form. Share to everyone for your reference, specific as follows:
A long time ago when you developed a asp.net project: press ENTER on a server TextBox control, and the page will be refreshed again. Google later, found that this is asp.net2.0 for the form to handle the special set "Enter key" function, about asp.net ajax form of Enter key, you can view this article "ASP." NET AJAX-based Enter key submission issue. The previous two links are all about how we set the ENTER key to trigger events by default. Now there is a new demand is this, input personnel in the input when the press ENTER button does not submit the form (think is also reasonable, if the form of input box more, accidentally press the ENTER key page to back how many times? , unless you click the server-side submit button directly. Simply put, the ENTER key feature of the form element is removed. Here is my implementation:
First, preliminary analysis and implementation:
1, the page inherits a base class BasePage, the base class inherits from the page class, registers the specific server control's OnKeyDown script event 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) {ba Se.
OnInit (e); Cancelformcontrolenterkey (this.
Page.Form.Controls); ///<summary>///Here we add the client onkeydown Footstep event to the server control in form, preventing the server control from pressing the ENTER key to send a direct postback///</summary>///<param N Ame= "Controls" ></param> public static void Cancelformcontrolenterkey (ControlCollection controls) {foreach (C Ontrol 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;}}
"); }//htmlControl else if (item. GetType () = = typeof (System.Web.UI.HtmlControls.HtmlInputText)) {HtmlInputControl HTMLControl = Item as Htmlinputco
Ntrol; 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 Call}}}
In this way, the page that wants to cancel the "Enter key" function can only inherit the BasePage class.
2, the User control processing: My idea is to continue processing the Runat=server control inside the user control in the base class, the test is also passed.
3. HTML controls in the page and in the user control that do not have runat=server tags, add onkeydown events directly to these HTML controls.
The following 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 ">
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");
}
Next 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=" 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 Textbox,htmlinputtext and no runat=server tags of the HTML control and the combination of the three user control according to the above idea to press the ENTER key to run the effect is not a postback.
Ii. problems encountered in scripting improvements
Then I see if (Event.which | | event.keycode) {if (Event.which = 13) | | (Event.keycode = 13)) {return false;}} This sentence continues to appear, and kindly put it in the page encapsulated into JavaScript functions called Forbidinputkeydown (EV):
<script type= "Text/javascript" >
function forbidinputkeydown (EV) {
if (typeof (EV)!= "undefined") {
if (Ev.keycode | | ev.which) {
if Ev.keycode = | | ev.which = = {return false;
}}} </script>
Then the onkeydown's method corresponds to the event "Forbidinputkeydown" (for example, for a server-side TextBox control in a page that is rewritten as a webControl.Attributes.Add when registering a client event) ("OnKeyDown", "Forbidinputkeydown"), and strangely, this time, the page has been sent back again?! Then the script was debugged and the Forbidinputkeydown function was executed, but the form was submitted.
I looked at the script position and moved it from head to body, and the problem remained. And then wonder if the script is wrong? No, the script is right. Is there a problem with character? Is there a problem, this confidence is really not. Is the registration event wrong? Well......
I kao, suddenly, the registration event should be so written: onkeydown= "return Forbidinputkeydown (Event)", that is, the Forbidinputkeydown function in front of the return is good, or character Ah, embarrassed.
I hope this article will help you with asp.net# programming.