CommandEventArgs.CommandArgument 属性
CommandEventArgs.CommandArgument 属性
获取命令的参数。
[C#] public object CommandArgument {get;} 属性值
包含该命令参数的 System.Object。
备注
CommandArgument 可以包含由程序员设置的任何字符串。CommandArgument 属性通过允许您为该命令提供任何附加信息,对 CommandName 属性加以补充。例如,您可以将 CommandName 属性设置为 Sort,将 CommandArgument 属性设置为 Ascending,从而指定一个以升序进行排序的命令。
示例
[ C#] 下面的示例展示如何使用 CommandArgument 属性来确定要执行的命令的补充信息。
[C#]
<%@ Page Language="C#" AutoEventWireup="True" %>
<html>
<head>
<script runat="server">
void CommandBtn_Click(Object sender, CommandEventArgs e)
{
switch(e.CommandName)
{
case "Sort":
// Call the method to sort the list.
Sort_List((String)e.CommandArgument);
break;
case "Submit":
// Display a message for the Submit button being clicked.
Message.Text = "You clicked the Submit button";
// Test whether the command argument is an empty string ("").
if((String)e.CommandArgument == "")
{
// End the message.
Message.Text += ".";
}
else
{
// Display an error message for the command argument.
Message.Text += ", however the command argument is not recogized.";
}
break;
default:
// The command name is not recognized. Display an error message.
Message.Text = "Command name not recogized.";
break;
}
}
void Sort_List(string commandArgument)
{
switch(commandArgument)
{
case "Ascending":
// Insert code to sort the list in ascending order here.
Message.Text = "You clicked the Sort Ascending button.";
break;
case "Descending":
// Insert code to sort the list in descending order here.
Message.Text = "You clicked the Sort Descending button.";
break;
default:
// The command argument is not recognized. Display an error message.
Message.Text = "Command argument not recogized.";
break;
}
}
</script>
</head>
<body>
<form runat="server">
<h3>Button CommandName Example</h3>
Click on one of the command buttons.
<br><br>
<asp:Button id="Button1"
Text="Sort Ascending"
CommandName="Sort"
CommandArgument="Ascending"
OnCommand="CommandBtn_Click"
runat="server"/>
<asp:Button id="Button2"
Text="Sort Descending"
CommandName="Sort"
CommandArgument="Descending"
OnCommand="CommandBtn_Click"
runat="server"/>
<br><br>
<asp:Button id="Button3"
Text="Submit"
CommandName="Submit"
OnCommand="CommandBtn_Click"
runat="server"/>
<asp:Button id="Button4"
Text="Unknown Command Name"
CommandName="UnknownName"
CommandArgument="UnknownArgument"
OnCommand="CommandBtn_Click"
runat="server"/>
<asp:Button id="Button5"
Text="Submit Unknown Command Argument"
CommandName="Submit"
CommandArgument="UnknownArgument"
OnCommand="CommandBtn_Click"
runat="server"/>
<br><br>
<asp:Label id="Message" runat="server"/>
</form>
</body>
</html>