.net的commandname领悟
生活随笔
收集整理的這篇文章主要介紹了
.net的commandname领悟
小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.
有時(shí)候需要幾個(gè)按紐調(diào)用同一模塊,實(shí)現(xiàn)相似(而非相同)的功能,需要用到按紐的CommandName行為
實(shí)現(xiàn)步驟如下:
1、VS2005新建一個(gè)網(wǎng)站項(xiàng)目,拖入兩個(gè)web控件Button控件,分別設(shè)置CommandName為btn1,btn2,btn3,btn4;再分別設(shè)置CommandArgument為Asc,Desc,Asc,Desc。等會(huì)看到,這兩個(gè)參數(shù)都會(huì)隨著B(niǎo)utton_Command事件傳入。再分別設(shè)置text屬性為數(shù)字升序,數(shù)字降序,字母升序,字母降序。
所得代碼如下:
<body>
??? <form id="form1" runat="server" method="post" action="Default.aspx">
??????? <asp:Button ID="Button1" runat="server" CommandName="btn1" Text="Button1" />
??????? <asp:Button ID="Button2" runat="server" CommandName="btn2" Text="Button2" /> ?????? <asp:Button ID="Button3" runat="server" CommandName="btn3" Text="Button3" /> ?????? <asp:Button ID="Button4" runat="server" CommandName="btn4" Text="Button4" />
??? </form>
</body> 2、選中Button1按紐,在屬性面板中選擇“事件”,在“操作”下雙擊“Command”欄,進(jìn)入代碼頁(yè),并輸入以下代碼:(把Button1_Command改為Button_Command) protected void Button_Command(object sender, CommandEventArgs e){switch (e.CommandName){case "btn1":Response.Write("數(shù)字:");ShowNumbers(e.CommandArgument);break;case "btn2":Response.Write("數(shù)字:");ShowNumbers(e.CommandArgument);break;case "btn3":Response.Write("字母");ShowLetters(e.CommandArgument);break;case "btn4":Response.Write("字母");ShowLetters(e.CommandArgument);break;}}private void ShowNumbers(object commandArgument){if (commandArgument.ToString() == "Asc"){Response.Write("升序:12345");}else if (commandArgument.ToString() == "Desc"){Response.Write("降序:54321");}} private void ShowLetters(object commandArgument){if (commandArgument.ToString() == "Asc"){Response.Write("ABCDE");}else if (commandArgument.ToString() == "Desc"){Response.Write("EDCBA");}}
3、回到前臺(tái)編輯頁(yè)面,分別選中Button1和Button2,將二者Command事件設(shè)置為Button_Command 4、運(yùn)行結(jié)果:點(diǎn)Button1顯示“btn1:12345”,點(diǎn)Button4顯示“btn2:EDCBA”。 5、總結(jié):在Button_Command事件中傳遞的e參數(shù),實(shí)際上是一個(gè)參數(shù)組,它包含e.CommandName(字符類(lèi)型)和e.CommandArgument(object類(lèi)型)。根據(jù)這兩個(gè)參數(shù),很輕易的把一組按紐分成四組功能:數(shù)字升序、數(shù)字降序、字母升序、字母降序。
??? <form id="form1" runat="server" method="post" action="Default.aspx">
??????? <asp:Button ID="Button1" runat="server" CommandName="btn1" Text="Button1" />
??????? <asp:Button ID="Button2" runat="server" CommandName="btn2" Text="Button2" /> ?????? <asp:Button ID="Button3" runat="server" CommandName="btn3" Text="Button3" /> ?????? <asp:Button ID="Button4" runat="server" CommandName="btn4" Text="Button4" />
??? </form>
</body> 2、選中Button1按紐,在屬性面板中選擇“事件”,在“操作”下雙擊“Command”欄,進(jìn)入代碼頁(yè),并輸入以下代碼:(把Button1_Command改為Button_Command) protected void Button_Command(object sender, CommandEventArgs e){switch (e.CommandName){case "btn1":Response.Write("數(shù)字:");ShowNumbers(e.CommandArgument);break;case "btn2":Response.Write("數(shù)字:");ShowNumbers(e.CommandArgument);break;case "btn3":Response.Write("字母");ShowLetters(e.CommandArgument);break;case "btn4":Response.Write("字母");ShowLetters(e.CommandArgument);break;}}private void ShowNumbers(object commandArgument){if (commandArgument.ToString() == "Asc"){Response.Write("升序:12345");}else if (commandArgument.ToString() == "Desc"){Response.Write("降序:54321");}} private void ShowLetters(object commandArgument){if (commandArgument.ToString() == "Asc"){Response.Write("ABCDE");}else if (commandArgument.ToString() == "Desc"){Response.Write("EDCBA");}}
3、回到前臺(tái)編輯頁(yè)面,分別選中Button1和Button2,將二者Command事件設(shè)置為Button_Command 4、運(yùn)行結(jié)果:點(diǎn)Button1顯示“btn1:12345”,點(diǎn)Button4顯示“btn2:EDCBA”。 5、總結(jié):在Button_Command事件中傳遞的e參數(shù),實(shí)際上是一個(gè)參數(shù)組,它包含e.CommandName(字符類(lèi)型)和e.CommandArgument(object類(lèi)型)。根據(jù)這兩個(gè)參數(shù),很輕易的把一組按紐分成四組功能:數(shù)字升序、數(shù)字降序、字母升序、字母降序。
總結(jié)
以上是生活随笔為你收集整理的.net的commandname领悟的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。
- 上一篇: 安装Lr11.0(LoadRunner)
- 下一篇: IIS5 IIS6 IIS7区别