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:
property String^ DefaultButton {
	String^ get ();
	void set (String^ 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

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

No code example is currently available or this language may not be supported.

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

ADD
Show:
© 2014 Microsoft