.NET Framework 類別庫
HtmlForm..::.DefaultButton 屬性

更新:2007 年 11 月

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

命名空間:  System.Web.UI.HtmlControls
組件:  System.Web (在 System.Web.dll 中)

語法

Visual Basic (宣告)
Public Property DefaultButton As String
Visual Basic (使用方式)
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);
}
J#
/** @property */
public String get_DefaultButton()
/** @property */
public  void set_DefaultButton(String value)
JScript
public function get DefaultButton () : String
public function set DefaultButton (value : String)
ASP.NET
<asp:HtmlForm DefaultButton="String" />

屬性值

型別:System..::.String

載入 HtmlForm 時,顯示為預設按鈕的按鈕控制項 ID。預設為空字串 ("")。

例外狀況

例外狀況條件
InvalidOperationException

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

備註

DefaultButton 屬性讓您指定使用者可以在表單的輸入控制項中 (例如文字方塊),按 ENTER 進行回傳。您可以將衍生自 IButtonControl 介面的任何控制項指定為預設按鈕,除了 LinkButton 控制項以外。如果 DefaultButton 屬性所參考的控制項不是衍生自 IButtonControl,便會擲回 InvalidOperationException 例外狀況。

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

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

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

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

  • LinkButton 控制項指定為預設按鈕。只有 ButtonImageButton 控制項會獲得支援。

  • 在非同步回傳期間,以程式設計方式變更 DefaultButton 屬性。將一個或多個 UpdatePanel 控制項加入至頁面,則可以在頁面上啟用非同步回傳。如需詳細資訊,請參閱UpdatePanel 控制項概觀網頁局部呈現概觀

範例

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

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

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

.NET Framework 和 .NET Compact Framework 並不支援各種平台的所有版本。如需支援平台版本的相關資訊,請參閱 .NET Framework 系統需求

版本資訊

.NET Framework

支援版本:3.5、3.0、2.0
請參閱

參考

其他資源

標記 :


Page view tracker