Export (0) Print
Expand All

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 string DefaultButton { get; set; }
<asp:HtmlForm DefaultButton="String" />

Property Value

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

ExceptionCondition
InvalidOperationException

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.

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="C#" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
    "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<script runat="server">     

  void Page_Load(object sender, System.EventArgs e)
  {

    // 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();
  }

</script>

<html xmlns="http://www.w3.org/1999/xhtml" >

<head>

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

</head>

<body>

  <form id="Form1"
        defaultbutton="SubmitButton"
        defaultfocus="TextBox1"
        runat="server">

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

    TextBox1:
    <asp:textbox id="TextBox1"
                 autopostback="true" 
                 runat="server">
    </asp:textbox>

    <br />

    TextBox2:
    <asp:textbox id="TextBox2"
                 autopostback="true" 
                 runat="server">
    </asp:textbox>

    <br /><br />

    <asp:button id="SubmitButton"
                text="Submit" 
                runat="server">
    </asp:button>

    <asp:button id="CancelButton" 
                text="Cancel"
                runat="server">
    </asp:button>

    <hr />

    <asp:label id="Label1"
               runat="Server">
    </asp:label>

    <asp:label id="Label2"
               runat="Server">
    </asp:label>

  </form>

</body>

</html>

.NET Framework

Supported in: 4.5, 4, 3.5, 3.0, 2.0

Windows 8.1, Windows Server 2012 R2, Windows 8, Windows Server 2012, Windows 7, Windows Vista SP2, Windows Server 2008 (Server Core Role not supported), Windows Server 2008 R2 (Server Core Role supported with SP1 or later; Itanium not supported)

The .NET Framework does not support all versions of every platform. For a list of the supported versions, see .NET Framework System Requirements.

Show:
© 2014 Microsoft