Share via


HtmlForm.DefaultButton 屬性

定義

取得或設定 HtmlForm 控制項的子控制項,它會在按下 ENTER 鍵時引發回傳。

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 時,顯示為預設按鈕的按鈕控制項 HtmlForm。 預設值為空字串 ("")。

例外狀況

當做預設按鈕參考的控制項不是 IButtonControl 型別。

範例

下列範例示範如何設定 屬性,以設定 DefaultButton 造成回傳的預設控制項。

<%@ 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 來指定回傳 (,例如文字方塊) 。 您可以將 指定為預設按鈕,任何衍生自 介面的 IButtonControl 控制項,但控制項除外 LinkButton 。 如果 屬性所參考的 DefaultButton 控制項不會衍生自 IButtonControlInvalidOperationException 則會擲回例外狀況。

如果您使用主版頁面,並從內容頁面設定 DefaultButton 屬性,請使用 UniqueID 按鈕的 IButtonControl 屬性。 如需主版頁面的詳細資訊,請參閱 ASP.NET 主版頁面

DefaultButton 下列案例中,屬性可能不會造成回傳:

  • 當焦點位於表單中的輸入控制項外部時,按 ENTER 鍵。 預設回傳動作不保證會觸發。

  • 當焦點位於多行文字方塊內時,按 ENTER 鍵。 在多行文字方塊中,預期的行為是按下 ENTER 會在文字方塊中建立新行。 在某些瀏覽器中,按多行文字方塊內的 ENTER 會觸發回傳。 在此情況下,如果您想要讓 ENTER 改為建立新行,您可以將 JavaScript 函式附加至輸入控制項。 腳本應該擷取 ENTER 鍵並停止回傳。 例如,您可以使用 Attributes 屬性集合來新增事件的用戶端腳本 onKeyPress

  • LinkButton 控制項指定為預設按鈕。 僅 Button 支援 和 ImageButton 控制項。

  • 在非同步回傳期間以程式設計方式變更 DefaultButton 屬性。 您可以在頁面上啟用非同步回傳,方法是將一或多個 UpdatePanel 控制項新增至頁面。 如需詳細資訊,請參閱 UpdatePanel 控制項概觀部分頁面轉譯概觀

適用於

另請參閱