使用 Button 控件在网页上创建下压按钮。
语法:<asp:Button />
既可以创建“提交”按钮,也可以创建“命令”按钮。
默认情况下,Button 控件是“提交”按钮。“提交”按钮没有与之相关联的命令名(由 CommandName 属性指定),它只是将网页回发到服务器。可以为 Click 事件提供事件处理程序,以便以编程方式控制在用户单击“提交”按钮时执行的操作。
通过设置 CommandName 属性,“命令”按钮可具有与之关联的命令名,例如 Sort。这使您可以在一个网页上创建多个 Button 控件,并以编程方式确定单击了哪个 Button 控件。您还可以将 CommandArgument 属性与命令按钮一起使用,提供有关要执行的命令的附加信息,例如 Ascending。可以为 Command 事件提供事件处理程序,以便以编程方式控制在用户单击“命令”按钮时执行的操作。
默认情况下,单击 Button 控件时执行页验证。页验证确定页上与验证控件关联的输入控件是否均通过该验证控件所指定的验证规则。若要禁止执行页验证,需将 CausesValidation 属性设置为 false。
下面的代码示例演示如何创建将网页内容回发到服务器的“提交”Button 控件。
1: <%@ Page Language="C#" AutoEventWireup="True" %>
2:
3: <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
4: "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
5: <html xmlns="http://www.w3.org/1999/xhtml" >
6: <head>
7: <title>Button Example</title>
8: <script language="C#" runat="server">
1:
2:
3: void SubmitBtn_Click(Object sender, EventArgs e)
4: {
5: Message.Text="Hello World!!";
6: }
7:
8:
</
script
>
9: </head>
10: <body>
11: <form id="form1" runat="server">
12:
13: <h3>Button Example</h3>
14:
15: Click on the submit button.<br /><br />
16:
17: <asp:Button id="Button1"
18: Text="Submit"
19: OnClick="SubmitBtn_Click"
20: runat="server"/>
21:
22: <br />
23:
24: <asp:label id="Message" runat="server"/>
25:
26: </form>
27: </body>
28: </html>
效果为:
点击Submit按钮,结果:
下面的代码示例演示如何创建对列表进行排序的“命令”Button 控件。
1: <%@ Page Language="C#" AutoEventWireup="True" %>
2:
3: <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
4: "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
5: <html xmlns="http://www.w3.org/1999/xhtml" >
6: <head runat="server">
7: <title>Button CommandName Example</title>
8: <script runat="server">
1:
2:
3: void CommandBtn_Click(Object sender, CommandEventArgs e)
4: {
5:
6: switch(e.CommandName)
7: {
8:
9: case "Sort":
10:
11: // Call the method to sort the list.
12: Sort_List((String)e.CommandArgument);
13: break;
14:
15: case "Submit":
16:
17: // Display a message for the Submit button being clicked.
18: Message.Text = "You clicked the Submit button";
19:
20: // Test whether the command argument is an empty string ("").
21: if((String)e.CommandArgument == "")
22: {
23: // End the message.
24: Message.Text += ".";
25: }
26: else
27: {
28: // Display an error message for the command argument.
29: Message.Text += ", however the command argument is not recogized.";
30: }
31: break;
32:
33: default:
34:
35: // The command name is not recognized. Display an error message.
36: Message.Text = "Command name not recogized.";
37: break;
38:
39: }
40:
41: }
42:
43: void Sort_List(string commandArgument)
44: {
45:
46: switch(commandArgument)
47: {
48:
49: case "Ascending":
50:
51: // Insert code to sort the list in ascending order here.
52: Message.Text = "You clicked the Sort Ascending button.";
53: break;
54:
55: case "Descending":
56:
57: // Insert code to sort the list in descending order here.
58: Message.Text = "You clicked the Sort Descending button.";
59: break;
60:
61: default:
62:
63: // The command argument is not recognized. Display an error message.
64: Message.Text = "Command argument not recogized.";
65: break;
66:
67: }
68:
69: }
70:
71:
</
script
>
9:
10: </head>
11:
12: <body>
13:
14: <form id="form1" runat="server">
15:
16: <h3>Button CommandName Example</h3>
17:
18: Click on one of the command buttons.
19:
20: <br /><br />
21:
22: <asp:Button id="Button1"
23: Text="Sort Ascending"
24: CommandName="Sort"
25: CommandArgument="Ascending"
26: OnCommand="CommandBtn_Click"
27: runat="server"/>
28:
29:
30: <asp:Button id="Button2"
31: Text="Sort Descending"
32: CommandName="Sort"
33: CommandArgument="Descending"
34: OnCommand="CommandBtn_Click"
35: runat="server"/>
36:
37: <br /><br />
38:
39: <asp:Button id="Button3"
40: Text="Submit"
41: CommandName="Submit"
42: OnCommand="CommandBtn_Click"
43: runat="server"/>
44:
45:
46: <asp:Button id="Button4"
47: Text="Unknown Command Name"
48: CommandName="UnknownName"
49: CommandArgument="UnknownArgument"
50: OnCommand="CommandBtn_Click"
51: runat="server"/>
52:
53:
54: <asp:Button id="Button5"
55: Text="Submit Unknown Command Argument"
56: CommandName="Submit"
57: CommandArgument="UnknownArgument"
58: OnCommand="CommandBtn_Click"
59: runat="server"/>
60:
61: <br /><br />
62:
63: <asp:Label id="Message" runat="server"/>
64:
65: </form>
66:
67: </body>
68: </html>
69:
效果为:
点击Sort Ascending按钮后:
点击Sort Descending按钮后:
点击其他按钮与上面类似。