![]() |
Web Development »
ASP.NET »
General
Intermediate
License: The Code Project Open License (CPOL)
Capturing the enter key to cause a button clickBy kubbenHave you ever wanted to trap the enter key press event in asp.net and have it click a button for you? This article describes two options for capturing the enter key press event and then clicking a button. One solution uses JavaScript, the other uses a panel control. |
Windows, .NET, ASP.NET, Visual Studio, Dev
|
||||||||
|
Advanced Search Add to IE Search |
|
|
|
||||||||||||||||
Have you ever wanted to trap the enter key press event in asp.net and have it click a button for you? This article describes two options for capturing the enter key press event and then clicking a button. One solution uses JavaScript, the other uses a panel control.
Most people like things to work a certain way. Consider a search engine text box. After you type in your search text, most people just want to push the enter key and have the search start. It is a pain when you have to grab the mouse and move the cursor to the search button and click it. The article describes two solutions to this problem.
Web pages have text boxes, text areas etc. where users enter text. Often after the text is entered the user needs to click a button. It may be a search button or a submit button or some other sort of button. The problem is how to trap the enter key press event so that the correct button can be clicked.
Let's explore two solutions to this problem. First let us look at using JavaScript. With this solution each textbox needs to have some JavaScript attached to the onKeyPress JavaScript event. I usually do this in the pageload method in the code behind.
//Add the javascript so we know where we want the enter key press to go
if (!IsPostBack)
{
txtboxFirstName.Attributes.Add("onKeyPress", "doClick('" + btnSearch.ClientID + "',event)");
txtboxLastName.Attributes.Add("onKeyPress", "doClick('" + btnSearch.ClientID + "',event)");
txtboxAddress1.Attributes.Add("onKeyPress", "doClick('" + btnSearch.ClientID + "',event)");
txtboxAddress2.Attributes.Add("onKeyPress", "doClick('" + btnSearch.ClientID + "',event)");
txtboxCity.Attributes.Add("onKeyPress", "doClick('" + btnSearch.ClientID + "',event)");
txtboxState.Attributes.Add("onKeyPress", "doClick('" + btnSearch.ClientID + "',event)");
txtboxZipcode.Attributes.Add("onKeyPress", "doClick('" + btnSearch.ClientID + "',event)");
}
NOTE: you pass into the JavaScript method the buttons ClientID so that the JavaScript method can find the button and call its click method.
Next we need a JavaScript method called doClick in the asp.net form.
<SCRIPT type=text/javascript>
function doClick(buttonName,e)
{
//the purpose of this function is to allow the enter key to
//point to the correct button to click.
var key;
if(window.event)
key = window.event.keyCode; //IE
else
key = e.which; //firefox
if (key == 13)
{
//Get the button the user wants to have clicked
var btn = document.getElementById(buttonName);
if (btn != null)
{ //If we find the button click it
btn.click();
event.keyCode = 0
}
}
}
</SCRIPT>
The next solution is using a Panel control. In this case the panel is doing all the work.
<asp:Panel ID="panSearch" runat="server" DefaultButton="btnSearch2" Width="100%" > <table width="100%"> <tr> <td>First Name</td> <td ><asp:TextBox ID="txtboxFirstName2" runat="server" ></asp:TextBox></td> </tr> ...
Notice that the Panel tag has a property called DefaultButton. You set this property to the button id of the button you want to be clicked on an enter key press event. So any text box inside of the panel will direct its enter key press to the button set in the DefaultButton property of the Panel.
I suppose we could spend a lot of time arguing about which method is better. Some people like JavaScript so that might be the better one. Others hate JavaScript and prefer writing less code, so they would choose the Panel solution. One thing I would note is that if you have several buttons and need to have different text boxes click different buttons then you have to go with the JavaScript solution since it gives you that flexibility. I guess I will let you decide which solution best fits your needs.
Hopefully this article demonstrates two useful techniques to capture the enter key press event and directing it to click the correct button. Let me know which method you prefer.
General
News
Question
Answer
Joke
Rant
Admin
|
PermaLink |
Privacy |
Terms of Use
Last Updated: 19 Jan 2007 Editor: |
Copyright 2007 by kubben Everything else Copyright © CodeProject, 1999-2009 Web22 | Advertise on the Code Project |