button

来源:互联网 发布:游戏公司的程序员累吗 编辑:程序博客网 时间:2024/05/01 04:06
在网页上显示下压按钮控件<AspNetHostingPermissionAttribute(SecurityAction.InheritanceDemand, Level := AspNetHostingPermissionLevel.Minimal)> _ <AspNetHostingPermissionAttribute(SecurityAction.LinkDemand, Level := AspNetHostingPermissionLevel.Minimal)> _ Public Class Button _     Inherits WebControl _     Implements IButtonControl, IPostBackEventHandler
Visual Basic(用法)
Dim instance As Button
C#
[AspNetHostingPermissionAttribute(SecurityAction.InheritanceDemand, Level = AspNetHostingPermissionLevel.Minimal)][AspNetHostingPermissionAttribute(SecurityAction.LinkDemand, Level = AspNetHostingPermissionLevel.Minimal)]public class Button : WebControl, IButtonControl,     IPostBackEventHandler
Visual C++
[AspNetHostingPermissionAttribute(SecurityAction::InheritanceDemand, Level = AspNetHostingPermissionLevel::Minimal)][AspNetHostingPermissionAttribute(SecurityAction::LinkDemand, Level = AspNetHostingPermissionLevel::Minimal)]public ref class Button : public WebControl,     IButtonControl, IPostBackEventHandler
J#
/** @attribute AspNetHostingPermissionAttribute(SecurityAction.InheritanceDemand, Level = AspNetHostingPermissionLevel.Minimal) *//** @attribute AspNetHostingPermissionAttribute(SecurityAction.LinkDemand, Level = AspNetHostingPermissionLevel.Minimal) */public class Button extends WebControl implements IButtonControl,     IPostBackEventHandler
JScript
public class Button extends WebControl implements IButtonControl, IPostBackEventHandler
ASP.NET
<asp:Button />

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

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

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

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

辅助功能

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

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

Visual Basic
复制代码
<%@ Page Language="VB" AutoEventWireup="True" %><!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"    "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"><html  ><head>    <title>Button Example</title><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 id="form1" runat="server">      <h3>Button Example</h3>      Click on the submit button.<br /><br />      <asp:Button id="Button1"           Text="Submit"           OnClick="SubmitBtn_Click"            runat="server"/>      <br />      <asp:label id="Message" runat="server"/>   </form></body></html>
C#
复制代码
<%@ Page Language="C#" AutoEventWireup="True" %><!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"    "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"><html  ><head>    <title>Button Example</title><script language="C#" runat="server">      void SubmitBtn_Click(Object sender, EventArgs e)       {         Message.Text="Hello World!!";      }   </script></head><body>   <form id="form1" runat="server">      <h3>Button Example</h3>      Click on the submit button.<br /><br />      <asp:Button id="Button1"           Text="Submit"           OnClick="SubmitBtn_Click"            runat="server"/>      <br />      <asp:label id="Message" runat="server"/>   </form></body></html>
JScript
复制代码
<%@ Page Language="JScript" AutoEventWireup="True" %><!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"    "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"><html  ><head>    <title>Button Example</title><script language="JScript" runat="server">      function SubmitBtn_Click(sender : Object, e : EventArgs)       {         Message.Text="Hello World!!";      }   </script></head><body>   <form id="form1" runat="server">      <h3>Button Example</h3>      Click on the submit button.<br /><br />      <asp:Button id="Button1"           Text="Submit"           OnClick="SubmitBtn_Click"            runat="server"/>      <br />      <asp:label id="Message" runat="server"/>   </form></body></html>

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

Visual Basic
复制代码
<%@ Page Language="VB" AutoEventWireup="True" %><!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"    "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"><html  ><head runat="server">    <title>Button CommandName Example</title><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 id="form1" 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>
C#
复制代码
<%@ Page Language="C#" AutoEventWireup="True" %><!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"    "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"><html  ><head runat="server">    <title>Button CommandName Example</title><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 id="form1" 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>