How to Set the Default Button for a Textbox in ASP.NET

Written by kevin lee
  • Share
  • Tweet
  • Share
  • Pin
  • Email
How to Set the Default Button for a Textbox in ASP.NET
Adding default buttons can help your site visitors work faster. (Stockbyte/Stockbyte/Getty Images)

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:

Things you need

  • Microsoft Visual Studio

Show MoreHide


  1. 1

    Launch Microsoft Visual Studio, then open one of your ASP.NET C# websites. Choose a project that has a text box.

  2. 2

    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.

  3. 3

    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.

  4. 4

    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.

  5. 5

    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:

    Response.Write("Button Clicked");

    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.

  6. 6

    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.

  7. 7

    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

Don't Miss

  • All types
  • Articles
  • Slideshows
  • Videos
  • Most relevant
  • Most popular
  • Most recent

No articles available

No slideshows available

No videos available

By using the site, you consent to the use of cookies. For more information, please see our Cookie policy.