*MORE* ON ASP.NET: How to create a Default "Enter" Button for Forms/PostBacks
Sponsored By
I lot of great ideas came in the discussion about How to create a Default "Enter" Button for Forms/PostBacks.
There are a number of good solutions, here they are collected, as well as an additional one from Dino Esposito.
- Similar to my idea, add a hidden EVENTTARGET, Darrell suggests to use the ClientID:
Page.RegisterHiddenField("__EVENTTARGET", btnSearch.ClientId) - Darrell Norton - Jeff uses a JavaScript function and adds an onkeydown handler to the text box's attributes:
Client-Side function:
function AnyInput_KeyDown (e, target)
{ if ((e.which && e.which == 13) || (e.keyCode && e.keyCode == 13))
{ __doPostBack(target, ''); return false; } return true; }
Caller: onkeydown -> "AnyInput_KeyDown(event,'btnSearch');" - Jeff Hulburt - Brian suggests Andy Smith's DefaultButton control.
- Gerry Heidenreich has some VB.NET code on his site that rolls the above ideas into the Page_Load.
- Phil Manijak suggests chaining events like:
private void SearchBox_TextChanged(object sender, EventArgs e) {
// Pass the event along, as if it were a button click.
this.SearchButton_Click(sender, e); } - Matt Berther has a great fix on his site that involves just adding a hidden text box with no name.
- Here's a great solution from Dino Esposito that takes the form of a Smart Text Box Server Control. PostTextBox.txt (5.57 KB)
About Scott
Scott Hanselman is a former professor, former Chief Architect in finance, now speaker, consultant, father, diabetic, and Microsoft employee. He is a failed stand-up comic, a cornrower, and a book author.
About Newsletter
August 31, 2004 21:17
Thanks for the summary post! Much better than constantly looking at the comments. :)
Comments are closed.