Skip to main content

How to Create a Default 'Enter' Button!


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")

Comments

Popular posts from this blog