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

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")%>' />  
<a href="/Actives/ActiveCodeMgr.aspx?ActionId=<%#Eval("ActionId")%>&isedit=1">修改</a>   
<a href="/Actives/ActiveCodeMgr.aspx?ActionId=<%#Eval("ActionId")%>&isshowprize=1">查看</a>  
<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)
kta{
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();
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" "/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="/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();
}
}
}

本文发布于:2024-09-22 15:42:02,感谢您对本站的认可!

本文链接:https://www.17tex.com/xueshu/137476.html

版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。

标签:活动   区分   回收站   删除   点击
留言与评论(共有 0 条评论)
   
验证码:
Copyright ©2019-2024 Comsenz Inc.Powered by © 易纺专利技术学习网 豫ICP备2022007602号 豫公网安备41160202000603 站长QQ:729038198 关于我们 投诉建议