.NET Framework Class Library ListViewCommandEventArgs Class

Inheritance Hierarchy
Namespace:
System.Web.UI.WebControls
Assembly:
System.Web.Extensions (in System.Web.Extensions.dll)

Syntax
Public Class ListViewCommandEventArgs _
Inherits CommandEventArgs
public class ListViewCommandEventArgs : CommandEventArgs
public ref class ListViewCommandEventArgs : public CommandEventArgs
type ListViewCommandEventArgs =
class
inherit CommandEventArgs
end
The ListViewCommandEventArgs type exposes the following members.

Constructors
|
| Name | Description |
|---|
.gif) | ListViewCommandEventArgs | Initializes a new instance of the ListViewCommandEventArgs class by using the specified source of the command and event arguments. | Top

Properties

Methods

Remarks
The ItemCommand event is raised when a button in the ListView control is clicked. This enables you to provide an event-handling method that performs a custom routine whenever this event occurs. Note |
|---|
The ListView control also raises other events when certain buttons are clicked (for example, buttons with the CommandName property set to "Delete", "Update", or "Page"). When you use one of these buttons, you can handle one of the specialized events that are provided by the control, such as the ItemDeleted or ItemDeleting events. |
A ListViewCommandEventArgs object is passed to the event-handling method, which enables you to determine the command name and command argument of the button that was clicked. To determine the command name use the CommandName property, and to determine the command argument, use the CommandArgument property. You can also access the button control that raised the event by using the CommandSource property. For a list of initial property values for an instance of the ListViewCommandEventArgs class, see the ListViewCommandEventArgs constructor.

Examples
The following example shows how to use the ListViewCommandEventArgs object that is passed to the event handler.
<%@ Page language="VB" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<script runat="server">
Protected Sub EmployeesListView_OnItemCommand(ByVal sender As Object, _
ByVal e As ListViewCommandEventArgs)
If String.Equals(e.CommandName, "AddToList") Then
' Verify that the employee ID is not already in the list. If not, add the
' employee to the list.
Dim dataItem As ListViewDataItem = CType(e.Item, ListViewDataItem)
Dim employeeID As String = _
EmployeesListView.DataKeys(dataItem.DisplayIndex).Value.ToString()
If SelectedEmployeesListBox.Items.FindByValue(employeeID) Is Nothing Then
Dim item As ListItem = _
New ListItem(e.CommandArgument.ToString(), employeeID)
SelectedEmployeesListBox.Items.Add(item)
End If
End If
End Sub
</script>
<html >
<head id="Head2" runat="server">
<title>Employee List</title>
</head>
<body>
<form id="form1" runat="server">
<asp:ListView runat="server"
ID="EmployeesListView"
OnItemCommand="EmployeesListView_OnItemCommand"
DataSourceID="EmployeesDataSource"
DataKeyNames="EmployeeID">
<LayoutTemplate>
<table runat="server" id="tblEmployees"
cellspacing="0" cellpadding="1" width="440px" border="1">
<tr id="itemPlaceholder" runat="server"></tr>
</table>
<asp:DataPager ID="EmployeesDataPager" runat="server" PageSize="10">
<Fields>
<asp:NumericPagerField />
</Fields>
</asp:DataPager>
</LayoutTemplate>
<ItemTemplate>
<tr runat="server">
<td>
<asp:Label runat="server" ID="NameLabel"
Text='<%#Eval("LastName") & ", " & Eval("FirstName") %>' />
</td>
<td style="width:80px">
<asp:LinkButton runat="server"
ID="SelectEmployeeButton"
Text="Add To List"
CommandName="AddToList"
CommandArgument='<%#Eval("LastName") & ", " & Eval("FirstName") %>' />
</td>
</tr>
</ItemTemplate>
</asp:ListView>
<br /><br />
<b>Selected Employees:</b><br />
<asp:ListBox runat="server" ID="SelectedEmployeesListBox" Rows="10" Width="300px" />
<!-- This example uses Microsoft SQL Server and connects -->
<!-- to the AdventureWorks sample database. Use an ASP.NET -->
<!-- expression to retrieve the connection string value -->
<!-- from the Web.config file. -->
<asp:SqlDataSource ID="EmployeesDataSource" runat="server"
ConnectionString="<%$ ConnectionStrings:AdventureWorks_DataConnectionString %>"
SelectCommand="SELECT [EmployeeID], [FirstName], [LastName]
FROM HumanResources.vEmployee
ORDER BY [LastName], [FirstName], [EmployeeID]">
</asp:SqlDataSource>
</form>
</body>
</html>
<%@ Page language="C#" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<script runat="server">
protected void EmployeesListView_OnItemCommand(object sender, ListViewCommandEventArgs e)
{
if (String.Equals(e.CommandName, "AddToList"))
{
// Verify that the employee ID is not already in the list. If not, add the
// employee to the list.
ListViewDataItem dataItem = (ListViewDataItem)e.Item;
string employeeID =
EmployeesListView.DataKeys[dataItem.DisplayIndex].Value.ToString();
if (SelectedEmployeesListBox.Items.FindByValue(employeeID) == null)
{
ListItem item = new ListItem(e.CommandArgument.ToString(), employeeID);
SelectedEmployeesListBox.Items.Add(item);
}
}
}
</script>
<html >
<head id="Head1" runat="server">
<title>Employee List</title>
</head>
<body>
<form id="form1" runat="server">
<asp:ListView runat="server"
ID="EmployeesListView"
OnItemCommand="EmployeesListView_OnItemCommand"
DataSourceID="EmployeesDataSource"
DataKeyNames="EmployeeID">
<LayoutTemplate>
<table runat="server" id="tblEmployees"
cellspacing="0" cellpadding="1" width="440px" border="1">
<tr id="itemPlaceholder" runat="server"></tr>
</table>
<asp:DataPager ID="EmployeesDataPager" runat="server" PageSize="10">
<Fields>
<asp:NumericPagerField />
</Fields>
</asp:DataPager>
</LayoutTemplate>
<ItemTemplate>
<tr runat="server">
<td>
<asp:Label runat="server" ID="NameLabel"
Text='<%#Eval("LastName") + ", " + Eval("FirstName") %>' />
</td>
<td style="width:80px">
<asp:LinkButton runat="server"
ID="SelectEmployeeButton"
Text="Add To List"
CommandName="AddToList"
CommandArgument='<%#Eval("LastName") + ", " + Eval("FirstName") %>' />
</td>
</tr>
</ItemTemplate>
</asp:ListView>
<br /><br />
<b>Selected Employees:</b><br />
<asp:ListBox runat="server" ID="SelectedEmployeesListBox" Rows="10" Width="300px" />
<!-- This example uses Microsoft SQL Server and connects -->
<!-- to the AdventureWorks sample database. Use an ASP.NET -->
<!-- expression to retrieve the connection string value -->
<!-- from the Web.config file. -->
<asp:SqlDataSource ID="EmployeesDataSource" runat="server"
ConnectionString="<%$ ConnectionStrings:AdventureWorks_DataConnectionString %>"
SelectCommand="SELECT [EmployeeID], [FirstName], [LastName]
FROM HumanResources.vEmployee
ORDER BY [LastName], [FirstName], [EmployeeID]">
</asp:SqlDataSource>
</form>
</body>
</html>

Version Information
.NET FrameworkSupported in: 4, 3.5

Platforms
Windows 7, Windows Vista SP1 or later, Windows XP SP3, Windows Server 2008 (Server Core Role not supported), Windows Server 2008 R2 (Server Core Role not supported), Windows Server 2003 SP2
The .NET Framework does not support all versions of every platform. For a list of the supported versions, see .NET Framework System Requirements.

Thread Safety
Any public static ( Shared in Visual Basic) members of this type are thread safe. Any instance members are not guaranteed to be thread safe.

See Also
|
Bibliothèque de classes .NET Framework ListViewCommandEventArgs, classe

Hiérarchie d'héritage
Espace de noms :
System.Web.UI.WebControls
Assembly :
System.Web.Extensions (dans System.Web.Extensions.dll)

Syntaxe
Public Class ListViewCommandEventArgs _
Inherits CommandEventArgs
public class ListViewCommandEventArgs : CommandEventArgs
public ref class ListViewCommandEventArgs : public CommandEventArgs
type ListViewCommandEventArgs =
class
inherit CommandEventArgs
end
Le type ListViewCommandEventArgs expose les membres suivants.

Constructeurs
|
| Nom | Description |
|---|
.gif) | ListViewCommandEventArgs | Initialise une nouvelle instance de la classe ListViewCommandEventArgs à l'aide de la source spécifiée de la commande et des arguments d'événement. | Début

Propriétés

Méthodes

Notes
L'événement ItemCommand est déclenché lorsque vous cliquez sur un bouton du contrôle ListView. Cela vous permet de fournir une méthode de gestion d'événements qui exécute une routine personnalisée lorsque cet événement se produit. Remarque |
|---|
Le contrôle ListView déclenche également d'autres événements lorsque vous cliquez sur certains boutons (par exemple, des boutons dont la propriété CommandName a la valeur « Delete », « Page » ou « Update »). Lorsque vous utilisez l'un de ces boutons, vous pouvez gérer l'un des événements spécialisés fournis par le contrôle, tels que les événements ItemDeleted ou ItemDeleting. |
Un objet ListViewCommandEventArgs est passé à la méthode de gestion d'événements qui vous permet de déterminer le nom de commande et l'argument de commande du bouton sur lequel vous cliquez. Pour déterminer le nom de la commande, utilisez la propriété CommandName et, pour déterminer l'argument de la commande, utilisez la propriété CommandArgument. Vous pouvez également accéder au contrôle bouton qui a déclenché l'événement en utilisant la propriété CommandSource. Pour obtenir la liste des valeurs initiales des propriétés d'une instance de la classe ListViewCommandEventArgs, consultez le constructeur ListViewCommandEventArgs.

Exemples
L'exemple suivant indique comment utiliser l'objet ListViewCommandEventArgs passé au gestionnaire d'événements.
<%@ Page language="VB" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<script runat="server">
Protected Sub EmployeesListView_OnItemCommand(ByVal sender As Object, _
ByVal e As ListViewCommandEventArgs)
If String.Equals(e.CommandName, "AddToList") Then
' Verify that the employee ID is not already in the list. If not, add the
' employee to the list.
Dim dataItem As ListViewDataItem = CType(e.Item, ListViewDataItem)
Dim employeeID As String = _
EmployeesListView.DataKeys(dataItem.DisplayIndex).Value.ToString()
If SelectedEmployeesListBox.Items.FindByValue(employeeID) Is Nothing Then
Dim item As ListItem = _
New ListItem(e.CommandArgument.ToString(), employeeID)
SelectedEmployeesListBox.Items.Add(item)
End If
End If
End Sub
</script>
<html >
<head id="Head2" runat="server">
<title>Employee List</title>
</head>
<body>
<form id="form1" runat="server">
<asp:ListView runat="server"
ID="EmployeesListView"
OnItemCommand="EmployeesListView_OnItemCommand"
DataSourceID="EmployeesDataSource"
DataKeyNames="EmployeeID">
<LayoutTemplate>
<table runat="server" id="tblEmployees"
cellspacing="0" cellpadding="1" width="440px" border="1">
<tr id="itemPlaceholder" runat="server"></tr>
</table>
<asp:DataPager ID="EmployeesDataPager" runat="server" PageSize="10">
<Fields>
<asp:NumericPagerField />
</Fields>
</asp:DataPager>
</LayoutTemplate>
<ItemTemplate>
<tr runat="server">
<td>
<asp:Label runat="server" ID="NameLabel"
Text='<%#Eval("LastName") & ", " & Eval("FirstName") %>' />
</td>
<td style="width:80px">
<asp:LinkButton runat="server"
ID="SelectEmployeeButton"
Text="Add To List"
CommandName="AddToList"
CommandArgument='<%#Eval("LastName") & ", " & Eval("FirstName") %>' />
</td>
</tr>
</ItemTemplate>
</asp:ListView>
<br /><br />
<b>Selected Employees:</b><br />
<asp:ListBox runat="server" ID="SelectedEmployeesListBox" Rows="10" Width="300px" />
<!-- This example uses Microsoft SQL Server and connects -->
<!-- to the AdventureWorks sample database. Use an ASP.NET -->
<!-- expression to retrieve the connection string value -->
<!-- from the Web.config file. -->
<asp:SqlDataSource ID="EmployeesDataSource" runat="server"
ConnectionString="<%$ ConnectionStrings:AdventureWorks_DataConnectionString %>"
SelectCommand="SELECT [EmployeeID], [FirstName], [LastName]
FROM HumanResources.vEmployee
ORDER BY [LastName], [FirstName], [EmployeeID]">
</asp:SqlDataSource>
</form>
</body>
</html>
<%@ Page language="C#" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<script runat="server">
protected void EmployeesListView_OnItemCommand(object sender, ListViewCommandEventArgs e)
{
if (String.Equals(e.CommandName, "AddToList"))
{
// Verify that the employee ID is not already in the list. If not, add the
// employee to the list.
ListViewDataItem dataItem = (ListViewDataItem)e.Item;
string employeeID =
EmployeesListView.DataKeys[dataItem.DisplayIndex].Value.ToString();
if (SelectedEmployeesListBox.Items.FindByValue(employeeID) == null)
{
ListItem item = new ListItem(e.CommandArgument.ToString(), employeeID);
SelectedEmployeesListBox.Items.Add(item);
}
}
}
</script>
<html >
<head id="Head1" runat="server">
<title>Employee List</title>
</head>
<body>
<form id="form1" runat="server">
<asp:ListView runat="server"
ID="EmployeesListView"
OnItemCommand="EmployeesListView_OnItemCommand"
DataSourceID="EmployeesDataSource"
DataKeyNames="EmployeeID">
<LayoutTemplate>
<table runat="server" id="tblEmployees"
cellspacing="0" cellpadding="1" width="440px" border="1">
<tr id="itemPlaceholder" runat="server"></tr>
</table>
<asp:DataPager ID="EmployeesDataPager" runat="server" PageSize="10">
<Fields>
<asp:NumericPagerField />
</Fields>
</asp:DataPager>
</LayoutTemplate>
<ItemTemplate>
<tr runat="server">
<td>
<asp:Label runat="server" ID="NameLabel"
Text='<%#Eval("LastName") + ", " + Eval("FirstName") %>' />
</td>
<td style="width:80px">
<asp:LinkButton runat="server"
ID="SelectEmployeeButton"
Text="Add To List"
CommandName="AddToList"
CommandArgument='<%#Eval("LastName") + ", " + Eval("FirstName") %>' />
</td>
</tr>
</ItemTemplate>
</asp:ListView>
<br /><br />
<b>Selected Employees:</b><br />
<asp:ListBox runat="server" ID="SelectedEmployeesListBox" Rows="10" Width="300px" />
<!-- This example uses Microsoft SQL Server and connects -->
<!-- to the AdventureWorks sample database. Use an ASP.NET -->
<!-- expression to retrieve the connection string value -->
<!-- from the Web.config file. -->
<asp:SqlDataSource ID="EmployeesDataSource" runat="server"
ConnectionString="<%$ ConnectionStrings:AdventureWorks_DataConnectionString %>"
SelectCommand="SELECT [EmployeeID], [FirstName], [LastName]
FROM HumanResources.vEmployee
ORDER BY [LastName], [FirstName], [EmployeeID]">
</asp:SqlDataSource>
</form>
</body>
</html>

Informations de version
.NET FrameworkPris en charge dans : 4, 3.5

Plateformes
Windows 7, Windows Vista SP1 ou ultérieur, Windows XP SP3, Windows Server 2008 (installation minimale non prise en charge), Windows Server 2008 R2 (installation minimale prise en charge avec SP1 ou version ultérieure), Windows Server 2003 SP2
Le .NET Framework ne prend pas en charge toutes les versions de chaque plateforme. Pour obtenir la liste des versions prises en charge, consultez Configuration requise du .NET Framework.

Sécurité des threads
Tous les membres static ( Shared en Visual Basic) publics de ce type sont thread-safe. Il n'est pas garanti que les membres d'instance soient thread-safe.

Voir aussi
RéférenceAutres ressources
|