CommandEventArgs.CommandArgument 属性 dodo

CommandEventArgs.CommandArgument 属性

获取命令的参数。

[Visual Basic]
Public ReadOnly Property CommandArgument As Object
[C#]
public object CommandArgument {get;}
[C++]
public: __property Object* get_CommandArgument();
[JScript]
public function get CommandArgument() : Object;

属性值

包含该命令参数的 System.Object

备注

CommandArgument 可以包含由程序员设置的任何字符串。CommandArgument 属性通过允许您为该命令提供任何附加信息,对 CommandName 属性加以补充。例如,您可以将 CommandName 属性设置为 Sort,将 CommandArgument 属性设置为 Ascending,从而指定一个以升序进行排序的命令。

示例

[Visual Basic, C#] 下面的示例展示如何使用 CommandArgument 属性来确定要执行的命令的补充信息。

[Visual Basic]
<%@ Page Language="VB" AutoEventWireup="True" %>
<html>
<head>
<script runat="server">
Sub CommandBtn_Click(sender As Object, e As CommandEventArgs)
Select e.CommandName
Case "Sort"
' Call the method to sort the list.
Sort_List(CType(e.CommandArgument, String))
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 CType(e.CommandArgument , String) = "" Then
' End the message.
Message.Text &= "."
Else
' Display an error message for the command argument.
Message.Text &= ", however the command argument is not recogized."
End If
Case Else
' The command name is not recognized. Display an error message.
Message.Text = "Command name not recogized."
End Select
End Sub
Sub Sort_List(commandArgument As String)
Select commandArgument
Case "Ascending"
' Insert code to sort the list in ascending order here.
Message.Text = "You clicked the Sort Ascending button."
Case "Descending"
' Insert code to sort the list in descending order here.
Message.Text = "You clicked the Sort Descending button."
Case Else
' The command argument is not recognized. Display an error message.
Message.Text = "Command argument not recogized."
End Select
End Sub
</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"/>
&nbsp;
<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"/>
&nbsp;
<asp:Button id="Button4"
Text="Unknown Command Name"
CommandName="UnknownName"
CommandArgument="UnknownArgument"
OnCommand="CommandBtn_Click"
runat="server"/>
&nbsp;
<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>
[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"/>
&nbsp;
<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"/>
&nbsp;
<asp:Button id="Button4"
Text="Unknown Command Name"
CommandName="UnknownName"
CommandArgument="UnknownArgument"
OnCommand="CommandBtn_Click"
runat="server"/>
&nbsp;
<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>

[C++, JScript] 没有可用于 C++ 或 JScript 的示例。若要查看 Visual Basic 或 C# 示例,请单击页左上角的“语言筛选器”按钮 语言筛选器

要求

平台: Windows 2000, Windows XP Professional, Windows Server 2003 系列

请参见

CommandEventArgs 类 | CommandEventArgs 成员 | System.Web.UI.WebControls 命名空间 | Button.CommandArgument | CommandArgument | CommandEventArgs 成员(Visual J# 语法) | C++ 托管扩展

原文地址:https://www.cnblogs.com/zgqys1980/p/448797.html