Export (0) Print
Expand All
1 out of 63 rated this helpful - Rate this topic

HtmlForm.DefaultButton Property

Note: This property is new in the .NET Framework version 2.0.

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; }
/** @property */
public String get_DefaultButton ()

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

public function get DefaultButton () : String

public function set DefaultButton (value : String)

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

InvalidOperationException

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

The button specified as the default button posts back to the server if the user presses the ENTER key after the page is loaded without navigating to a different control on the form. You can specify any control that raises an event when it is clicked for the DefaultButton property. If the control referenced by the DefaultButton property is not of the type IButtonControl, an InvalidOperationException exception is thrown.

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

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

<%@ page language="C#" %>

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

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

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

.NET Framework

Supported in: 2.0
Did you find this helpful?
(1500 characters remaining)
Thank you for your feedback

Community Additions

ADD
Show:
© 2014 Microsoft. All rights reserved.