下面介绍一个小例子了解自定义按钮如何使用:
.aspx代码如下:
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="自定义按钮.aspx.cs" Inherits="自定义按钮" %> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head runat="server"> <title></title> </head> <body> <form id="form1" runat="server"> <div> <asp:Button ID="Button1" runat="server" Text="Button" OnCommand="mycmd" CommandArgument="1" CommandName="group1" /><%--CommandName用于分组--%> <asp:Button ID="Button3" runat="server" Text="Button" OnCommand="mycmd" CommandArgument="2" CommandName="group1" /><%--CommandName用于分组--%> <asp:Button ID="Button4" runat="server" Text="Button" OnCommand="mycmd" CommandArgument="3" CommandName="group1" /><%--CommandName用于分组--%> <asp:Button ID="Button2" runat="server" Text="Button" OnCommand="mycmd" CommandArgument="1" CommandName="group2" /><%--CommandName用于分组--%> </div> </form> </body> </html>
.aspx.cs代码如下:
using System; using System.Collections.Generic; using System.Linq; using System.Web; using System.Web.UI; using System.Web.UI.WebControls; public partial class 自定义按钮 : System.Web.UI.Page { protected void Page_Load(object sender, EventArgs e) { } protected void mycmd(object sender,CommandEventArgs e) { switch (e.CommandName) { case "group1": Response.Write(e.CommandArgument); break; case "group2": Response.Write(e.CommandName); break; } } }
效果如下:
时间: 2024-10-27 03:34:43