This article is demonstrating how to wire up an enter key into a textbox. For example you have a search text box where you press enter then it will click go button and at the same page you have another textbox where you want to do another button click when you press the enter which means it’ is not necessary to post the page. This java script is used to capture the key event of enter and execute the LinkButton and ASP.NET button Click Event on the server side. Please add this javascript to your javascript common library or add this to your master page. This piece of code works in Firefox as well
function ButtonKeyPress(evt, thisElementName) { if(evt.which || evt.keyCode) { if ((evt.which == 13) || (evt.keyCode == 13)) { // alert('post back href: ' +document.getElementById(thisElementName).href); if (typeof document.getElementById(thisElementName).href != 'undefined') { location = document.getElementById(thisElementName).href; } else { document.getElementById(thisElementName).click(); } return false; } } else { return true; } }
And add this to your .NET code behind on the page load
Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load If Not Page.IsPostBack Then If (User.Identity.IsAuthenticated) Then Response.Redirect("frmTimeMain.aspx") End If txtGUID.Attributes.Add("onkeydown", "ButtonKeyPress(event, '" + lnkSubmit.ClientID + "')") txtPassword.Attributes.Add("onkeydown", "ButtonKeyPress(event, '" + lnkSubmit.ClientID + "')") End If End Sub