While visiting a website, you may have wondered how pressing "Enter" after typing something into a text box caused some action to occur. Your browser even sends you to new websites when you enter URLs into the address bar and press "Enter" instead of clicking a button. If you create ASP.NET websites, you can duplicate this useful behaviour by setting your ASP.NET form's "DefaultButton" property. Once it's set, site visitors can submit forms and trigger automated button clicks by simply pressing "Enter" after typing information into a text box.
- Skill level:
Other People Are Reading
Things you need
- Microsoft Visual Studio
Launch Microsoft Visual Studio, then open one of your ASP.NET C# websites. Choose a project that has a text box.
Click the "View" menu item at the top of Visual Studio, then click "Toolbox" to open the toolbox. Locate the "Button" control in the toolbox, and drag it onto the form. Visual Studio adds the button to the form. The button's name appears above the button. Remember that name.
Press "F4." The "Properties" window opens. Locate the drop-down arrow at the top of that window then click "<FORM>." The form's properties appear below the drop-down arrow in the window.
Find the "DefaultButton" property. A text box appears next to that property. Type the name of your new button in that text box. For instance, if the button's name is "Button1," type "Button1" (without the quotes) in the text box. Doing this sets the form's "DefaultButton" property to that button.
Locate that button on the form and double-click it. Visual Studio opens a code window. This code window displays the method that runs when users click the button. The method's name consists of the button's name followed by "_Click." For instance, if the button's name is "Button1," the method's name is "Button1_Click." Paste the following code inside this method:
This causes the browser to write the text string "Button Clicked" when anyone clicks the button. Adding this code allows you to test the form's "DefaultButton" property.
Press your "F5" key to launch your project. Visual Studio opens the Web form in your browser. Click the button you added. The words "Button Clicked" appear on the Web page because your click triggered the button's "Click" event code. That code, as shown previously, executes the "Response.Write" method that writes "Button Clicked" text to the Web page.
Type something in the text box you added, then press "Enter." The browser displays "Button Clicked" again because the form clicked the button automatically when you pressed "Enter." This behaviour saves users time because they do not have to click the button after filling out the text box.
Tips and warnings
- If your form has multiple buttons, choose the one you would like to use as the default by adding its name to the form's "DefaultButton" property as shown in this example. Since there is only one property, only one button can become the default button at a time. In real life, you will probably delete the "Response.Write" method included in this example for testing purposes and replace it with code that performs a useful task
- 20 of the funniest online reviews ever
- 14 Biggest lies people tell in online dating sites
- Hilarious things Google thinks you're trying to search for