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){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和CommandArgumentprotected void btn1_Command(object sender, CommandEventArgs e) {e.CommandArgument.ToString();}} }
?
轉載于:https://www.cnblogs.com/zxx193/p/3471482.html
總結
以上是生活随笔為你收集整理的ASP.NET中Button控件的CommandName和CommandArgument属性用法的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: RTX2009管理器服务运行状态空白
- 下一篇: 自己的部分小软件合计 2000 - 20