asp.net中commandname应用
生活随笔
收集整理的這篇文章主要介紹了
asp.net中commandname应用
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
http://blog.sina.com.cn/s/blog_6672120701010y7e.html
有時候需要幾個按紐調用同一模塊,實現相似(而非相同)的功能,需要用到按紐的CommandName行為 實現步驟如下: 1、VS2005新建一個網站項目,拖入兩個web控件Button控件,分別設置CommandName為btn1,btn2,btn3,btn4;再分別設置CommandArgument為Asc,Desc,Asc,Desc。等會看到,這兩個參數都會隨著Button_Command事件傳入。再分別設置text屬性為數字升序,數字降序,字母升序,字母降序。 所得代碼如下: <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”欄,進入代碼頁,并輸入以下代碼:(把Button1_Command改為Button_Command) protected void Button_Command(object sender, CommandEventArgs e)
{
switch (e.CommandName)
{
case "btn1":
Response.Write("數字:");
ShowNumbers(e.CommandArgument);
break;
case "btn2":
Response.Write("數字:");
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、回到前臺編輯頁面,分別選中Button1和Button2,將二者Command事件設置為Button_Command 4、運行結果:點Button1顯示“btn1:12345”,點Button4顯示“btn2:EDCBA”。 5、總結:在Button_Command事件中傳遞的e參數,實際上是一個參數組,它包含e.CommandName(字符類型)和e.CommandArgument(object類型)。根據這兩個參數,很輕易的把一組按紐分成四組功能:數字升序、數字降序、字母升序、字母降序。
有時候需要幾個按紐調用同一模塊,實現相似(而非相同)的功能,需要用到按紐的CommandName行為 實現步驟如下: 1、VS2005新建一個網站項目,拖入兩個web控件Button控件,分別設置CommandName為btn1,btn2,btn3,btn4;再分別設置CommandArgument為Asc,Desc,Asc,Desc。等會看到,這兩個參數都會隨著Button_Command事件傳入。再分別設置text屬性為數字升序,數字降序,字母升序,字母降序。 所得代碼如下: <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”欄,進入代碼頁,并輸入以下代碼:(把Button1_Command改為Button_Command) protected void Button_Command(object sender, CommandEventArgs e)
{
switch (e.CommandName)
{
case "btn1":
Response.Write("數字:");
ShowNumbers(e.CommandArgument);
break;
case "btn2":
Response.Write("數字:");
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、回到前臺編輯頁面,分別選中Button1和Button2,將二者Command事件設置為Button_Command 4、運行結果:點Button1顯示“btn1:12345”,點Button4顯示“btn2:EDCBA”。 5、總結:在Button_Command事件中傳遞的e參數,實際上是一個參數組,它包含e.CommandName(字符類型)和e.CommandArgument(object類型)。根據這兩個參數,很輕易的把一組按紐分成四組功能:數字升序、數字降序、字母升序、字母降序。
總結
以上是生活随笔為你收集整理的asp.net中commandname应用的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 对灰色按钮克星的屏蔽
- 下一篇: 此计算机上缺少vc2005_sp1_wi