Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

In ASP.net Webforms how do you detect which Textbox someone pressed enter?

In ASP.net Webforms how do you detect which Textbox someone pressed enter?

Please no Javascript answers.

I need to handle it all in the code behind using VB.NET.

like image 366
Brian Boatright Avatar asked Sep 17 '08 20:09

Brian Boatright


3 Answers

Why do you need to determine the which TextBox was pressed? Are you looking to see which TextBox was being focused so that you can trigger the proper button click event?

If you are looking to do something like this, one trick I've done was to "group" the appropriate form elements within their own panel and then set the "DefaultButton" property accordingly.

Doing this allows me to have a "Search by Name", "Search by Department", "Search by Id", etc. Textbox/Button combination on a single form and still allow the user to type their query parameter, hit Enter, and have the proper search method get invoked in the code behind.

like image 69
Dillie-O Avatar answered Dec 01 '22 01:12

Dillie-O


I suspect it cannot be done without javascript - when you hit enter, the browser submits the form - it doesn't submit what field had the focus. So unless you use JS to add that information to the form being submitted, you're out of luck.

like image 20
zigdon Avatar answered Dec 01 '22 00:12

zigdon


Without using Javascript, you just can't. That information is not conveyed from the client browser to the server.

like image 33
Forgotten Semicolon Avatar answered Dec 01 '22 00:12

Forgotten Semicolon