HtmlForm.DefaultButton 属性

定义

获取或设置按 Enter 键时会引起回发的 HtmlForm 控件的子控件。

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 控件概述分页呈现概述

适用于

另请参阅