HtmlForm.DefaultButton 屬性

定義

取得或設定 HtmlForm 控制項的子控制項,它會在按下 ENTER 鍵時引發回傳。Gets or sets the child control of the HtmlForm control that causes postback when the ENTER key is pressed.

public:
 property System::String ^ DefaultButton { System::String ^ get(); void set(System::String ^ value); };
public string DefaultButton { get; set; }
member this.DefaultButton : string with get, set
Public Property DefaultButton As String

屬性值

載入 ID 時,顯示為預設按鈕的按鈕控制項 HtmlFormThe ID of the button control to display as the default button when the HtmlForm is loaded. 預設值為空字串 ("")。The default value is an empty string ("").

例外狀況

當做預設按鈕參考的控制項不是 IButtonControl 型別。The control referenced as the default button is not of the type IButtonControl.

範例

下列範例顯示如何設定 DefaultButton 屬性,以設定會造成回傳的預設控制項。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>
<%@ 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 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>

備註

DefaultButton 屬性可讓您指定使用者可以在表單中的輸入控制項(例如文字方塊)中按 ENTER 鍵,以引發回傳。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). 除了 LinkButton 控制項以外,您可以指定做為預設按鈕,任何衍生自 IButtonControl 介面的控制項。You can specify as a default button any control that derives from the IButtonControl interface except the LinkButton control. 如果 DefaultButton 屬性所參考的控制項不是衍生自 IButtonControl,則會擲回 InvalidOperationException 例外狀況。If the control that is referenced by the DefaultButton property does not derive from IButtonControl, an InvalidOperationException exception is thrown.

如果您使用主版頁面,並從 [內容] 頁面設定 [DefaultButton] 屬性,請使用 [IButtonControl] 按鈕的 [UniqueID] 屬性。If you are using master pages and you set the DefaultButton property from a content page, use the UniqueID property of the IButtonControl button. 如需主版頁面的詳細資訊,請參閱ASP.NET 主版頁面For more information about master pages, see ASP.NET Master Pages.

在下列案例中,DefaultButton 屬性可能不會造成回傳:The DefaultButton property might not cause a postback in the following scenarios:

  • 當焦點位於表單中的輸入控制項之外時,按 ENTER 鍵。Pressing ENTER when focus is outside the input controls in the form. 不保證會觸發預設的回傳動作。The default postback action is not guaranteed to be triggered.

  • 當焦點在多行文字方塊內時,按 ENTER 鍵。Pressing ENTER when focus is inside a multi-line text box. 在多行文字方塊中,預期的行為是按下 ENTER 會在文字方塊中建立新的一行。In a multi-line text box, the expected behavior is that pressing ENTER creates a new line in the text box. 在某些瀏覽器中,在多行文字方塊內按 ENTER 會觸發回傳。In some browsers, pressing ENTER inside a multi-line text box triggers a postback. 在此情況下,如果您想要輸入改為建立新的行,您可以將 JavaScript 函式附加至輸入控制項。In that case, if you want ENTER to create a new line instead, you can attach a JavaScript function to the input control. 腳本應該會捕獲 ENTER 鍵並停止回傳。The script should capture the ENTER key and stop the postback. 例如,您可以使用 Attributes 屬性集合來加入 onKeyPress 事件的用戶端腳本。For example, you can use the Attributes property collection to add client script for the onKeyPress event.

  • LinkButton 控制項指定為預設按鈕。Specifying a LinkButton control as a default button. 僅支援 ButtonImageButton 控制項。Only Button and ImageButton controls are supported.

  • 在非同步回傳期間以程式設計方式變更 DefaultButton 屬性。Changing the DefaultButton property programmatically during an asynchronous postback. 藉由將一或多個 UpdatePanel 控制項加入至頁面,即可在頁面上啟用非同步回傳。Asynchronous postbacks can be enabled on a page by adding one or more UpdatePanel controls to the page. 如需詳細資訊,請參閱UpdatePanel 控制項總覽部分頁面呈現總覽For more information, see UpdatePanel Control Overview and Partial-Page Rendering Overview.

適用於

另請參閱