ASP.NET中Button控件的CommandName和CommandArgument属性用法

在Repeater中的使用:

            <asp:Repeater ID="rptActionList" runat="server" OnItemCommand="rptActionList_ItemCommand" OnItemDataBound="rptActionList_DataBinding">

                <ItemTemplate>

                    <tr class="info">

                        <td>

                            <%#Eval("ActionName") %>

                        </td>

                        <td>

                             <asp:LinkButton ID="btn_Down_Exchanged" runat="server" Text="下载" CommandName="downloadExchanged"

                                CommandArgument='<%#Eval("ActionId")+"$"+Eval("ActionName")%>' />

                        </td>

                        <td>

                            <asp:LinkButton ID="btn_Del" runat="server" Text="删除" OnClientClick="return confirm('确认删除活动吗?')" CommandName="del" CommandArgument='<%#Eval("ActionId")%>' />&nbsp;&nbsp;
<a href="/Actives/ActiveCodeMgr.aspx?ActionId=<%#Eval("ActionId")%>&isedit=1">修改</a> &nbsp;&nbsp;
<a href="/Actives/ActiveCodeMgr.aspx?ActionId=<%#Eval("ActionId")%>&isshowprize=1">查看</a>&nbsp;&nbsp;
<asp:LinkButton runat="server" ID="lbAddRecycled" CommandName="add" CommandArgument='<%#Eval("ActionId") %>'>放进回收站</asp:LinkButton> <asp:LinkButton runat="server" ID="lbRemoveRecycled" CommandName="remove" CommandArgument='<%#Eval("ActionId") %>'>移出回收站</asp:LinkButton> </td> <td> <asp:LinkButton ID="btn_Down" runat="server" Text="导出激活码" CommandName="download" CommandArgument='<%#Eval("ActionId")+"$"+Eval("ActionName")%>' /> </td> </tr> </ItemTemplate> </asp:Repeater>

        protected void rptActionList_ItemCommand(object source, RepeaterCommandEventArgs e)

        {

            if (e.CommandName.ToString() == "del") //通过判断CommandName来区分执行什么操作

            {

                int ActionId = int.Parse(e.CommandArgument.ToString());

                if (ActionId > 0)

                {

                    int ret = activeCodeNewMgr.DelAction(ActionId);

                    if (ret != Const.SUCCESS)

                    {

                        LogHelper.Error("活动删除失败:ActionId:" + ActionId);

                    }

                    else

                    {

                        BindData();

                    }

                }

            }

            else if (e.CommandName.ToString() == "download")

            {

                string[] args = e.CommandArgument.ToString().Split('$');

                int _ActionId = int.Parse(args[0]);

                if (_ActionId > 0)

                {

                    List<ActivationInfo> list = activeCodeNewMgr.GetActivationByActionId(_ActionId, 0, 1, 500000);

                    string _actionName = string.Empty;

                    if (args.Length > 1) _actionName = args[1];

                    this.DownLoadActvation(list, _actionName);

                }

                else

                {

                    JSAlert("活动不存在!!");

                }

            }

            else if (e.CommandName == "add")

            {

                int actionId = Convert.ToInt32(e.CommandArgument);

                activeCodeNewMgr.ChangedActivety(1, actionId);

                this.BindData();

            }

            else if (e.CommandName == "remove")

            {

                int actionId = Convert.ToInt32(e.CommandArgument);

                activeCodeNewMgr.ChangedActivety(0, actionId);

                this.BindData();

            }

            else if (e.CommandName == "downloadExchanged")

            {

                string[] args = e.CommandArgument.ToString().Split('$');

                int _ActionId = int.Parse(args[0]);

                int count = 0;

                if (_ActionId > 0)

                {

                    List<ActivationInfo> list = activeCodeNewMgr.GetActioncodeExchanged(_ActionId, out count);

                    if (list != null && list.Count > 0)

                    {

                        foreach (ActivationInfo ai in list)

                        {

                            UserProfile userInfo = new UserProfile();

                            userInfo.getUserInfo(ai.UserId);

                            ai.NickName = userInfo.UserNickName;

                        }

                    }



                    string _actionName = string.Empty;

                    if (args.Length > 1) _actionName = args[1];

                    this.DownLoadActionCodeExchanged(list, _actionName);

                }

                else

                {

                    JSAlert("活动不存在!!");

                }

            }

        }

 

另外的用法:让点击不同按钮的事件共用一个,通过判断CommandName和CommandArgument来区分

<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="ButtonTest.aspx.cs" Inherits="WebFormTest.TestCollect.ButtonTest" %>



<!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="btn1" runat="server" Text="按钮1" CommandName="1" 

            onclick="btn1_Click" CommandArgument="11" oncommand="btn1_Command" />

        <br />

        <asp:Button ID="btn2" runat="server" Text="按钮2" CommandName="2" 

            onclick="btn1_Click" CommandArgument="22" />

        <br />

        <asp:Button ID="Button3" runat="server" Text="按钮3" oncommand="btn1_Command" />

    </div>

    </form>

</body>

</html>

 

using System;

using System.Collections;

using System.Configuration;

using System.Data;

using System.Linq;

using System.Web;

using System.Web.Security;

using System.Web.UI;

using System.Web.UI.HtmlControls;

using System.Web.UI.WebControls;

using System.Web.UI.WebControls.WebParts;

using System.Xml.Linq;



namespace WebFormTest.TestCollect

{

    public partial class ButtonTest : System.Web.UI.Page

    {

        protected void Page_Load(object sender, EventArgs e)

        {

        }



        protected void btn1_Click(object sender, EventArgs e)

        {

            

            string commandName = ((Button)sender).CommandName;



            if (commandName == "1")

            {

                Response.Write("1");

            }

            else if (commandName == "2")

            {

                Response.Write("2");

            }



            string commandArgument = ((Button)sender).CommandArgument;

            if (commandArgument == "11")

            {

                Response.Write("11");

            }

            else if (commandArgument == "22")

            {

                Response.Write("22");

            }



        }


//OnClick与OnCommand都是点击执行某事件,OnCommand中的CommandEventArgs e,参数e,附带属性CommandName和CommandArgument
protected void btn1_Command(object sender, CommandEventArgs e) { e.CommandArgument.ToString(); } } }

 

你可能感兴趣的:(asp.net)