HtmlForm.DefaultButton Property

Gets or sets the child control of the HtmlForm control that causes postback when the ENTER key is pressed.

Namespace: System.Web.UI.HtmlControls
Assembly: System.Web (in system.web.dll)

Public Property DefaultButton As String
Dim instance As HtmlForm
Dim value As String

value = instance.DefaultButton

instance.DefaultButton = value
/** @property */
public String get_DefaultButton ()

/** @property */
public void set_DefaultButton (String value)

public function get DefaultButton () : String

public function set DefaultButton (value : String)

Not applicable.

Property Value

The ID of the button control to display as the default button when the HtmlForm is loaded. The default value is an empty string ("").

Exception typeCondition


The control referenced as the default button is not of the type IButtonControl.

The DefaultButton property lets you specify that users can cause a postback by pressing ENTER in an input control in the form (such as a text box). You can specify as a default button any control that derives from the IButtonControl interface except the LinkButton control. If the control that is referenced by the DefaultButton property does not derive from IButtonControl, an InvalidOperationException exception is thrown.

If you are using master pages and you set the DefaultButton property from a content page, use the UniqueID property of the IButtonControl button. For more information about master pages, see ASP.NET Master Pages Overview.

The DefaultButton property might not cause a postback in the following scenarios:

  • Pressing ENTER when focus is outside the input controls in the form. The default postback action is not guaranteed to be triggered.

  • Pressing ENTER when focus is inside a multi-line text box. In a multi-line text box, the expected behavior is that pressing ENTER creates a new line in the text box. In some browsers, pressing ENTER inside a multi-line text box triggers a postback. In that case, if you want ENTER to create a new line instead, you can attach a JavaScript function to the input control. The script should capture the ENTER key and stop the postback. For example, you can use the Attributes property collection to add client script for the onKeyPress event.

  • Specifying a LinkButton control as a default button. Only Button and ImageButton controls are supported.

  • Changing the DefaultButton property programmatically during an asynchronous postback. Asynchronous postbacks can be enabled on a page by adding one or more UpdatePanel controls to the page. For more information, see UpdatePanel Control Overview and Partial-Page Rendering Overview.

The following example shows how to set the DefaultButton property to set the default control that causes a postback.

<%@ Page Language="VB" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
<script runat="Server">
  Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs)
    ' Set the text of the two label controls.
    Label1.Text = "The DefaultButton property is set to " _
                  & Form1.DefaultButton.ToString & "<br/>"
    Label2.Text = "The DefaultFocus property is set to " _
                  & Form1.DefaultFocus.ToString
  End Sub

<html xmlns="" >


    <title>HtmlForm DefaultButton and DefaultFocus Properties Example</title>



  <form id="Form1"
    <h3>HtmlForm DefaultButton and DefaultFocus Properties Example</h3>        
    <asp:textbox id="TextBox1"
    <br />
    <asp:textbox id="TextBox2"
    <br /><br />
    <asp:button id="SubmitButton"
    <asp:button id="CancelButton" 
    <hr />
    <asp:label id="Label1"
    <asp:label id="Label2"




Windows 98, Windows Server 2000 SP4, Windows Server 2003, Windows XP Media Center Edition, Windows XP Professional x64 Edition, Windows XP SP2, Windows XP Starter Edition

The Microsoft .NET Framework 3.0 is supported on Windows Vista, Microsoft Windows XP SP2, and Windows Server 2003 SP1.

.NET Framework

Supported in: 3.0, 2.0

Community Additions