Button 类

在网页上显示下压按钮控件。

**命名空间:**System.Web.UI.WebControls
**程序集:**System.Web(在 system.web.dll 中)

语法

声明
Public Class Button
    Inherits WebControl
    Implements IButtonControl, IPostBackEventHandler
用法
Dim instance As Button
public class Button : WebControl, IButtonControl, IPostBackEventHandler
public ref class Button : public WebControl, IButtonControl, IPostBackEventHandler
public class Button extends WebControl implements IButtonControl, IPostBackEventHandler
public class Button extends WebControl implements IButtonControl, IPostBackEventHandler

备注

使用 Button 控件在网页上创建下压按钮。既可以创建“提交”按钮,也可以创建“命令”按钮。

默认情况下,Button 控件是“提交”按钮。“提交”按钮没有与之相关联的命令名(由 CommandName 属性指定),它只是将网页回发到服务器。可以为 Click 事件提供事件处理程序,以便以编程方式控制在用户单击“提交”按钮时执行的操作。

通过设置 CommandName 属性,“命令”按钮可具有与之关联的命令名,例如 Sort。这使您可以在一个网页上创建多个 Button 控件,并以编程方式确定单击了哪个 Button 控件。您还可以将 CommandArgument 属性与命令按钮一起使用,提供有关要执行的命令的附加信息,例如 Ascending。可以为 Command 事件提供事件处理程序,以便以编程方式控制在用户单击“命令”按钮时执行的操作。

默认情况下,单击 Button 控件时执行页验证。页验证确定页上与验证控件关联的输入控件是否均通过该验证控件所指定的验证规则。若要禁止执行页验证,需将 CausesValidation 属性设置为 false

辅助功能

默认情况下,为此控件呈现的标记可能不符合辅助功能标准,例如 Web 内容辅助功能准则 1.0 (WCAG) 优先级 1 准则。有关此控件的辅助功能支持的详细信息,请参见 ASP.NET 控件和辅助功能

主题 位置
演练:为 ASP.NET Web 服务器控件创建基本控件设计器 创作 ASP.NET 控件
演练:使用 ASP.NET 用户控件创建可重用元素 在 Visual Studio 中构建 ASP .NET Web 应用程序
如何:在 ASP.NET 网页中创建事件处理程序 (Visual Studio) 在 Visual Studio 中构建 ASP .NET Web 应用程序
演练:到自定义业务对象的数据绑定 在 Visual Studio 中构建 ASP .NET Web 应用程序
如何:向 Web 窗体页添加按钮 Web 服务器控件 (Visual Studio) 在 Visual Studio 中构建 ASP .NET Web 应用程序
演练:在 Visual Web Developer 中使用级联样式表样式 在 Visual Studio 中构建 ASP .NET Web 应用程序
演练:验证 Web 窗体页中的用户输入 在 Visual Studio 中构建 ASP .NET Web 应用程序
演练:在 Visual Web Developer 中创建和使用 ASP.NET 母版页 在 Visual Studio 中构建 ASP .NET Web 应用程序
演练:在 Visual Web Developer 中创建基本网页 在 Visual Studio 中构建 ASP .NET Web 应用程序
演练:在 Visual Studio 中使用主题自定义网站 在 Visual Studio 中构建 ASP .NET Web 应用程序
演练:使用 ASP.NET 用户控件创建可重用元素 使用 Visual Web Developer 生成应用程序
演练:在 Visual Web Developer 中创建和使用 ASP.NET 母版页 使用 Visual Web Developer 生成应用程序
演练:验证 Web 窗体页中的用户输入 使用 Visual Web Developer 生成应用程序
演练:在 Visual Studio 中使用主题自定义网站 使用 Visual Web Developer 生成应用程序
如何:响应客户端脚本中的 Button Web 服务器控件事件 在 Visual Studio 中生成 ASP .NET Web 应用程序
演练:为 Web 服务器控件创建基本控件设计器 在 Visual Studio 中生成 ASP .NET Web 应用程序
如何:将多个事件连接到 ASP.NET 网页中的单个事件处理程序 在 Visual Studio 中生成 ASP .NET Web 应用程序
如何:确定引发事件的 Web 服务器控件 在 Visual Studio 中生成 ASP .NET Web 应用程序
如何:在 ASP.NET Web 服务器控件上设置焦点 在 Visual Studio 中生成 ASP .NET Web 应用程序
如何:在 ASP.NET 网页中创建事件处理程序 (Visual Studio) 在 Visual Studio 中生成 ASP .NET Web 应用程序
演练:到自定义业务对象的数据绑定 在 Visual Studio 中生成 ASP .NET Web 应用程序
演练:在 Visual Web Developer 中创建基本网页 在 Visual Studio 中生成 ASP .NET Web 应用程序
如何:响应 Button Web 服务器控件事件 在 Visual Studio 中生成 ASP .NET Web 应用程序
如何:响应数据绑定控件中的按钮事件 在 Visual Studio 中生成 ASP .NET Web 应用程序
如何:响应 DataList 或 Repeater 项中的按钮事件 在 Visual Studio 中生成 ASP .NET Web 应用程序
如何:响应客户端脚本中的 Button Web 服务器控件事件 在 Visual Studio 中生成 ASP .NET Web 应用程序
演练:为 Web 服务器控件创建基本控件设计器 在 Visual Studio 中生成 ASP .NET Web 应用程序
如何:将多个事件连接到 ASP.NET 网页中的单个事件处理程序 在 Visual Studio 中生成 ASP .NET Web 应用程序
如何:确定引发事件的 Web 服务器控件 在 Visual Studio 中生成 ASP .NET Web 应用程序
如何:在 ASP.NET Web 服务器控件上设置焦点 在 Visual Studio 中生成 ASP .NET Web 应用程序
如何:在 ASP.NET 网页中创建事件处理程序 (Visual Studio) 在 Visual Studio 中生成 ASP .NET Web 应用程序
演练:到自定义业务对象的数据绑定 在 Visual Studio 中生成 ASP .NET Web 应用程序
演练:在 Visual Web Developer 中创建基本网页 在 Visual Studio 中生成 ASP .NET Web 应用程序
如何:向 Web 窗体页添加按钮 Web 服务器控件 (Visual Studio) 在 Visual Studio 中生成 ASP .NET Web 应用程序
如何:响应 Button Web 服务器控件事件 在 Visual Studio 中生成 ASP .NET Web 应用程序
如何:响应数据绑定控件中的按钮事件 在 Visual Studio 中生成 ASP .NET Web 应用程序
如何:响应 DataList 或 Repeater 项中的按钮事件 在 Visual Studio 中生成 ASP .NET Web 应用程序
如何:向 Web 窗体页添加按钮 Web 服务器控件 (Visual Studio) 在 Visual Studio 中生成 ASP .NET Web 应用程序
如何:响应客户端脚本中的 Button Web 服务器控件事件 生成 ASP .NET Web 应用程序
如何:将多个事件连接到 ASP.NET 网页中的单个事件处理程序 生成 ASP .NET Web 应用程序
如何:确定引发事件的 Web 服务器控件 生成 ASP .NET Web 应用程序
如何:向 Web 窗体页添加按钮 Web 服务器控件 生成 ASP .NET Web 应用程序
如何:在 ASP.NET Web 服务器控件上设置焦点 生成 ASP .NET Web 应用程序
如何:向 Web 窗体页添加 PlaceHolder Web 服务器控件 生成 ASP .NET Web 应用程序
如何:响应 Button Web 服务器控件事件 生成 ASP .NET Web 应用程序
如何:响应数据绑定控件中的按钮事件 生成 ASP .NET Web 应用程序
如何:响应 DataList、Repeater 或 GridView 项中的按钮事件 生成 ASP .NET Web 应用程序

示例

下面的代码示例演示如何创建将网页内容回发到服务器的“提交”Button 控件。

<%@ Page Language="VB" AutoEventWireup="True" %>

<html>
<head>
   <script language="VB" runat=server>
    Sub SubmitBtn_Click(sender As Object, e As EventArgs)
        Message.Text = "Hello World!!"
    End Sub 'SubmitBtn_Click
  </script>
</head>
<body>
   <form runat="server">

      <h3>Button Example</h3>

      Click on the submit button.<br><br>
 
      <asp:Button id="Button1"
           Text="Submit"
           OnClick="SubmitBtn_Click" 
           runat="server"/>
       
      <p>

      <asp:label id="Message" runat="server"/>
 
   </form>
</body>
</html>
<%@ Page Language="C#" AutoEventWireup="True" %>

<html>
<head>
   <script language="C#" runat=server>

      void SubmitBtn_Click(Object sender, EventArgs e) 
      {
         Message.Text="Hello World!!";
      }

   </script>
</head>
<body>
   <form runat="server">

      <h3>Button Example</h3>

      Click on the submit button.<br><br>
 
      <asp:Button id="Button1"
           Text="Submit"
           OnClick="SubmitBtn_Click" 
           runat="server"/>
       
      <p>

      <asp:label id="Message" runat="server"/>
 
   </form>
</body>
</html>
<%@ Page Language="JScript" AutoEventWireup="True" %>

<html>
<head>
   <script language="JScript" runat=server>

      function SubmitBtn_Click(sender : Object, e : EventArgs) 
      {
         Message.Text="Hello World!!";
      }

   </script>
</head>
<body>
   <form runat="server">

      <h3>Button Example</h3>

      Click on the submit button.<br><br>
 
      <asp:Button id="Button1"
           Text="Submit"
           OnClick="SubmitBtn_Click" 
           runat="server"/>
       
      <p>

      <asp:label id="Message" runat="server"/>
 
   </form>
</body>
</html>

下面的代码示例演示如何创建对列表进行排序的“命令”Button 控件。

<%@ Page Language="VB" AutoEventWireup="True" %>

<html>
<head>

   <script runat="server">

      Sub CommandBtn_Click(sender As Object, e As CommandEventArgs) 

         Select e.CommandName

            Case "Sort"

               ' Call the method to sort the list.
               Sort_List(CType(e.CommandArgument, String))

            Case "Submit"

               ' Display a message for the Submit button being clicked.
               Message.Text = "You clicked the Submit button"

               ' Test whether the command argument is an empty string ("").
               If CType(e.CommandArgument , String) = "" Then
              
                  ' End the message.
                  Message.Text &= "."
               
               Else
               
                  ' Display an error message for the command argument. 
                  Message.Text &= ", however the command argument is not recogized."
               
               End If                

            Case Else

               ' The command name is not recognized. Display an error message.
               Message.Text = "Command name not recogized."

         End Select

      End Sub

      Sub Sort_List(commandArgument As String)

         Select commandArgument

            Case "Ascending"
 
               ' Insert code to sort the list in ascending order here.
               Message.Text = "You clicked the Sort Ascending button."

            Case "Descending"
              
               ' Insert code to sort the list in descending order here.
               Message.Text = "You clicked the Sort Descending button."

            Case Else
        
               ' The command argument is not recognized. Display an error message.
               Message.Text = "Command argument not recogized."

         End Select

      End Sub

   </script>

</head>
 
<body>

   <form runat="server">

      <h3>Button CommandName Example</h3>

      Click on one of the command buttons.

      <br><br>
 
      <asp:Button id="Button1"
           Text="Sort Ascending"
           CommandName="Sort"
           CommandArgument="Ascending"
           OnCommand="CommandBtn_Click" 
           runat="server"/>

      &nbsp;

      <asp:Button id="Button2"
           Text="Sort Descending"
           CommandName="Sort"
           CommandArgument="Descending"
           OnCommand="CommandBtn_Click" 
           runat="server"/>

      <br><br>

      <asp:Button id="Button3"
           Text="Submit"
           CommandName="Submit"
           OnCommand="CommandBtn_Click" 
           runat="server"/>

      &nbsp;

      <asp:Button id="Button4"
           Text="Unknown Command Name"
           CommandName="UnknownName"
           CommandArgument="UnknownArgument"
           OnCommand="CommandBtn_Click" 
           runat="server"/>

      &nbsp;

      <asp:Button id="Button5"
           Text="Submit Unknown Command Argument"
           CommandName="Submit"
           CommandArgument="UnknownArgument"
           OnCommand="CommandBtn_Click" 
           runat="server"/>
       
      <br><br>

      <asp:Label id="Message" runat="server"/>
 
   </form>
 
</body>
</html>
<%@ Page Language="C#" AutoEventWireup="True" %>

<html>
<head>

   <script runat="server">

      void CommandBtn_Click(Object sender, CommandEventArgs e) 
      {

         switch(e.CommandName)
         {

            case "Sort":

               // Call the method to sort the list.
               Sort_List((String)e.CommandArgument);
               break;

            case "Submit":

               // Display a message for the Submit button being clicked.
               Message.Text = "You clicked the Submit button";

               // Test whether the command argument is an empty string ("").
               if((String)e.CommandArgument == "")
               {
                  // End the message.
                  Message.Text += ".";
               }
               else
               {
                  // Display an error message for the command argument. 
                  Message.Text += ", however the command argument is not recogized.";
               }                
               break;

            default:

               // The command name is not recognized. Display an error message.
               Message.Text = "Command name not recogized.";
               break; 

         }

      }

      void Sort_List(string commandArgument)
      {

         switch(commandArgument)
         {

            case "Ascending":
 
               // Insert code to sort the list in ascending order here.
               Message.Text = "You clicked the Sort Ascending button.";
               break;

            case "Descending":
              
               // Insert code to sort the list in descending order here.
               Message.Text = "You clicked the Sort Descending button.";
               break;

            default:
        
               // The command argument is not recognized. Display an error message.
               Message.Text = "Command argument not recogized.";
               break;

         }

      }

   </script>

</head>
 
<body>

   <form runat="server">

      <h3>Button CommandName Example</h3>

      Click on one of the command buttons.

      <br><br>
 
      <asp:Button id="Button1"
           Text="Sort Ascending"
           CommandName="Sort"
           CommandArgument="Ascending"
           OnCommand="CommandBtn_Click" 
           runat="server"/>

      &nbsp;

      <asp:Button id="Button2"
           Text="Sort Descending"
           CommandName="Sort"
           CommandArgument="Descending"
           OnCommand="CommandBtn_Click" 
           runat="server"/>

      <br><br>

      <asp:Button id="Button3"
           Text="Submit"
           CommandName="Submit"
           OnCommand="CommandBtn_Click" 
           runat="server"/>

      &nbsp;

      <asp:Button id="Button4"
           Text="Unknown Command Name"
           CommandName="UnknownName"
           CommandArgument="UnknownArgument"
           OnCommand="CommandBtn_Click" 
           runat="server"/>

      &nbsp;

      <asp:Button id="Button5"
           Text="Submit Unknown Command Argument"
           CommandName="Submit"
           CommandArgument="UnknownArgument"
           OnCommand="CommandBtn_Click" 
           runat="server"/>
       
      <br><br>

      <asp:Label id="Message" runat="server"/>
 
   </form>
 
</body>
</html>

.NET Framework 安全性

继承层次结构

System.Object
   System.Web.UI.Control
     System.Web.UI.WebControls.WebControl
      System.Web.UI.WebControls.Button

线程安全

此类型的任何公共静态(Visual Basic 中的 Shared)成员都是线程安全的,但不保证所有实例成员都是线程安全的。

平台

Windows 98、Windows 2000 SP4、Windows Server 2003、Windows XP Media Center Edition、Windows XP Professional x64 Edition、Windows XP SP2、Windows XP Starter Edition

.NET Framework 并不是对每个平台的所有版本都提供支持。有关受支持版本的列表,请参见系统要求

版本信息

.NET Framework

受以下版本支持:2.0、1.1、1.0

请参见

参考

Button 成员
System.Web.UI.WebControls 命名空间
CommandName
CommandArgument
Click
Command

其他资源

Button Web 服务器控件