.NET Framework Class Library
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)
Syntax

Visual Basic (Declaration)
Public Property DefaultButton As String
Visual Basic (Usage)
Dim instance As HtmlForm
Dim value As String

value = instance.DefaultButton

instance.DefaultButton = value
C#
public string DefaultButton { get; set; }
Visual C++
public:
property String^ DefaultButton {
    String^ get ();
    void set (String^ value);
}
JScript
public function get DefaultButton () : String
public function set DefaultButton (value : String)
ASP.NET
<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 ("").
Exceptions

ExceptionCondition
InvalidOperationException

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

Remarks

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.

Examples

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

Visual Basic
<%@ Page Language="VB" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
    "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<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

 </script>

<html  >

<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>
C#
<%@ 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  >

<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>
Platforms

Windows 7, Windows Vista, Windows XP SP2, Windows XP Media Center Edition, Windows XP Professional x64 Edition, Windows XP Starter Edition, Windows Server 2008 R2, Windows Server 2008, Windows Server 2003, Windows Server 2000 SP4, Windows Millennium Edition, Windows 98

The .NET Framework and .NET Compact Framework do not support all versions of every platform. For a list of the supported versions, see .NET Framework System Requirements.
Version Information

.NET Framework

Supported in: 3.5, 3.0, 2.0
See Also

Reference

Other Resources

Tags :


Page view tracker