Normally in the controls like Text box at ASP.NET Webform, whenever you press enter key it will submit the page. In other words, it will post the page back to the server. In this post, we are going to explain different ways how to avoid this by using JavaScript.
#1. Insert the JavaScript Directly to The Markup
In this way, you need to include the “onkeydown” event on each text box control.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 |
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="WebForm1.aspx.cs" Inherits="DemoWebForm.WebForm1" %> <!DOCTYPE html> <html xmlns="http://www.w3.org/1999/xhtml"> <head runat="server"> <title></title> </head> <body> <form id="form1" runat="server"> Enter First Name: <asp:TextBox ID="TextBox1" runat="server" AutoPostBack="false" onkeydown="return (event.keyCode!=13);"></asp:TextBox> <br /> Enter Last Name: <asp:TextBox ID="TextBox2" runat="server" AutoPostBack="false" onkeydown="return (event.keyCode!=13);"></asp:TextBox> <br /> <asp:Button runat="server" ID="btnSubmit" Text="Submit" /> </form> </body> </html> |
#2. Insert JavaScript Through Server Side Code
If you want to achieve the above one in server side, that is you loop through the controls and inject the same javascript snippet. For this, you can use the following markup and code behind:
Markup:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 |
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="WebForm1.aspx.cs" Inherits="DemoWebForm.WebForm1" %> <!DOCTYPE html> <html xmlns="http://www.w3.org/1999/xhtml"> <head runat="server"> <title></title> </head> <body> <form id="form1" runat="server"> Enter First Name: <asp:TextBox ID="TextBox1" runat="server" AutoPostBack="false" ></asp:TextBox> <br /> Enter Last Name: <asp:TextBox ID="TextBox2" runat="server" AutoPostBack="false" ></asp:TextBox> <br /> <asp:Button runat="server" ID="btnSubmit" Text="Submit" /> </form> </body> </html> |
Code behind:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 |
using System; using System.Collections.Generic; using System.Linq; using System.Web; using System.Web.UI; using System.Web.UI.WebControls; namespace DemoWebForm { public partial class WebForm1 : System.Web.UI.Page { protected void Page_Load(object sender, EventArgs e) { DisableEnterKey(Page.Controls); } public void DisableEnterKey(ControlCollection ctrls) { foreach (Control c in ctrls) { foreach (Control ctrl in c.Controls) { if (ctrl is TextBox) ((TextBox)ctrl).Attributes.Add("onkeydown", "return (event.keyCode!=13);"); else if (ctrl is DropDownList) ((DropDownList)ctrl).Attributes.Add("onkeydown", "return (event.keyCode!=13);"); } } } } } |
#3. Using JQuery
In the above two techniques, either we need to include the same line in each control or we need to loop through the controls and inser the JavaScript. In some cases you may want to achieve the same using jQuery.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 |
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="WebForm1.aspx.cs" Inherits="DemoWebForm.WebForm1" %> <!DOCTYPE html> <html xmlns="http://www.w3.org/1999/xhtml"> <head runat="server"> <title></title> <script src="Scripts/jquery-1.10.2.js"></script> <script type="text/javascript"> $(document).ready(function(){ $('input[type=text]').on("keydown", "", function () { return (event.keyCode != 13); }); }); </script> </head> <body> <form id="form1" runat="server"> Enter First Name: <asp:TextBox ID="TextBox1" runat="server" AutoPostBack="false"></asp:TextBox> <br /> Enter Last Name: <asp:TextBox ID="TextBox2" runat="server" AutoPostBack="false"></asp:TextBox> <br /> <asp:Button runat="server" ID="btnSubmit" Text="Submit" /> </form> </body> </html> |
Simple Right?
Looking for ASP.NET Hosting on European Server? We can help you a lot!
hostforlifeasp.net is European Windows Hosting Provider which focuses on Windows Platform only. We deliver on-demand hosting solutions including Shared hosting, Reseller Hosting, Cloud Hosting, Dedicated Servers, and IT as a Service for companies of all sizes. We have customers from around the globe, spread across every continent. We serve the hosting needs of the business and professional, government and nonprofit, entertainment and personal use market segments.