PDA

Click to See Complete Forum and Search --> : Perform action when user presses <Enter> on keyboard.


token
Jan 17th, 2005, 03:31 PM
I have this simple search form and would like for the search button action to act when the user presses the <enter> key on the keyboard instead of having to click on the search button.

Can someone please tell me how to do this. I see there is an AccessKey button to associate a keyboard shortcut but do not know what value to place in there if that is the right solution.

Any help would be appreciated.

Thanks.

Strider
Jan 18th, 2005, 11:23 AM
prob the best way is to have only 1 button, because once you hit enter the button will br triggered.

Magiaus
Jan 18th, 2005, 11:53 AM
<asp:textbox id="txtSearch" onKeyUp="__doSearchReturn()" runat="server"></asp:textbox> <asp:button id="btnSearch" text="Search" runat="server"></asp:button>
<script language="JavaScript">
function __doSearchReturn(e)
{
if(e.keyCode)
{//ie
if(e.keyCode == 13){__doPostBack('btnSearch','');}
}
else
{//n4
if(e.which == 13){__doPostBack('btnSearch','');}
}
}
</script>

something like that....

Patch21
Jan 18th, 2005, 12:42 PM
Magiaus

Do you know what the equivalent for e.which is in Netscape 7? It doesn't seem to like it.

token
Jan 18th, 2005, 01:27 PM
Thanks again for answering Magiaus, I entered it in just like you have it there, but it still doesnt seem to work. Nothing happens.

Am I missing something?

token
Jan 18th, 2005, 01:28 PM
Oh, and 'OnKeyUP' doesnt seem to be recognized, it tells me: Could not find any attribute 'onKeyUp' of element 'Textbox'.

And one other thing is that the button is an <asp:imagebutton> if that makes any difference.

And to Strider:
I do only have one button, is there a property I am not setting?

token
Jan 18th, 2005, 01:39 PM
Okay, i found the solution, by searching the internet.
Simply put this line of code in your OnLoad event in your code-behind:

Page.RegisterHiddenField("__EVENTTARGET", "btnSearch")

and voila it works.

Thanks again everyone for helping me out.

Here is the page i found the solution on:
http://www.developer.com/net/asp/article.php/1594521

Magiaus
Jan 19th, 2005, 10:35 AM
Don't know about Navigator 7 I just checked in my ye old JavaScript book from like 5 years ago lol.