 |
|
|
|
Submit using Enter on multiple controls
|
|
|
 |
|
Location: Blogs Andy's Blog |
|
| Posted by: host |
10/9/2006 6:48 PM |
ASP.NET 2.0 adds the DefaultButton property, which allows you to specify which button causes a postback when the Enter key is pressed.
But what if you have multiple controls on the page that could potentially cause a postback? For example, maybe you have multiple search textboxes on your page - any of which the user should be able to press Enter on and cause a postback?
In the example below we will assume there is a textbox named "txtFind" and when the user hits <enter> on txtFind, we want to execute a Postback on btnFind.
First we add a client-side handler to the control that should cause the postback when enter is hit. You also pass in the ClientId of the element you want to cause the postback:
txtFind.Attributes.Add( "onkeydown", "return OnKeyDown(event, '" + btnFind.ClientID + "');");
Then we need to add some script to handle that event in both IE and Mozilla -
function OnKeyDown(e, submitButton)
{
var nKey = -1;
var sourceElement;
if (e && e.which)
nKey = e.which; // NS
else
if (window.event && window.event.keyCode)
nKey = window.event.keyCode; // IE
if (nKey == 13)
{
document.getElementById(submitButton).click();
return false;
}
return true;
}
|
|
| Permalink |
Trackback |
|
|
 |
|
 |
|
|