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
Continue Reading