在网页上显示下压按钮控件。
命名空间:System.Web.UI.WebControls
程序集:System.Web(在 system.web.dll 中)
Public Class Button
Inherits WebControl
Implements IButtonControl, IPostBackEventHandler
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 控件和辅助功能。
下面的代码示例演示如何创建将网页内容回发到服务器的“提交”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"/>
<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"/>
<asp:Button id="Button4"
Text="Unknown Command Name"
CommandName="UnknownName"
CommandArgument="UnknownArgument"
OnCommand="CommandBtn_Click"
runat="server"/>
<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"/>
<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"/>
<asp:Button id="Button4"
Text="Unknown Command Name"
CommandName="UnknownName"
CommandArgument="UnknownArgument"
OnCommand="CommandBtn_Click"
runat="server"/>
<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>
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