This is one of those little code snippets you can pull your hair out trying to find. And no help file nor book I’ve come across actually gives reference to it. So, surely it can’t be that important?
Imagine you’ve created an ASP.NET Web page with a search button. The user taps a phrase into a text box and presses Enter. On most regular Web pages (think: Google), the form would be submitted and the results returned. In other words, the search button is automatically “clicked” for you.
However on an ASP.NET Web page, pressing Enter resubmits the form to the server, but actually does nothing… which is pretty useless, really.
So, how do you set a default button to be clicked when the user presses Enter? Simply add the following line to your page’s Load event, replacing “btnSearch” with the name of your button. It uses a hidden Page method called RegisterHiddenField and works splendidly:
Page.RegisterHiddenField(“__EVENTTARGET”, “btnSearch”)
Related posts:
- How to Dynamically Create Images Ask any ASP developer who has ever tried to dynamically create his own images and he’ll tell you it’s a...
- Your free search engine – Microsoft Indexing Server Many web applications provide a search capability, which allows users to search all the content of a web application. This...
- Subclassing Pages and Master Pages in ASP.NET 2.0 Sometimes the simplest things in ASP.NET 2.0 turn out to be the hardest things to implement. One example is trying...
- The Secrets to Uploading Files with Ease In the golden, olden days of ASP, managing a file upload was pretty difficult. Most developers reverted to digging deep...
- Visual Studio 2005 Web Application Project Preview The Visual Studio 2005 Web Application Project Model is a new web project option for Visual Studio 2005 that provides...
