Cet article a fait l'objet d'une traduction manuelle. Déplacez votre pointeur sur les phrases de l'article pour voir la version originale de ce texte. |
Traduction
Source
|
DetailsViewCommandEventArgs, classe
Fournit des données pour l'événement ItemCommand.
System.EventArgs
System.Web.UI.WebControls.CommandEventArgs
System.Web.UI.WebControls.DetailsViewCommandEventArgs
Assembly : System.Web (dans System.Web.dll)
Le type DetailsViewCommandEventArgs expose les membres suivants.
| Nom | Description | |
|---|---|---|
|
DetailsViewCommandEventArgs | Initialise une nouvelle instance de la classe DetailsViewCommandEventArgs. |
| Nom | Description | |
|---|---|---|
|
CommandArgument | Obtient l'argument de la commande. (Hérité de CommandEventArgs.) |
|
CommandName | Obtient le nom de la commande. (Hérité de CommandEventArgs.) |
|
CommandSource | Obtient la source de la commande. |
| Nom | Description | |
|---|---|---|
|
Equals(Object) | Détermine si l'Object spécifié est égal à l'Object en cours. (Hérité de Object.) |
|
Finalize | Autorise un objet à tenter de libérer des ressources et d'exécuter d'autres opérations de netto***ge avant qu'il ne soit récupéré par l'opération garbage collection. (Hérité de Object.) |
|
GetHashCode | Sert de fonction de hachage pour un type particulier. (Hérité de Object.) |
|
GetType | Obtient le Type de l'instance actuelle. (Hérité de Object.) |
|
MemberwiseClone | Crée une copie superficielle de l'objet Object actif. (Hérité de Object.) |
|
ToString | Retourne une chaîne qui représente l'objet actuel. (Hérité de Object.) |
Le contrôle DetailsView déclenche l'événement ItemCommand lors d'un clic sur un bouton d'un champ de ligne ButtonField, CommandField ou TemplateField. Cela vous permet de fournir un gestionnaire d'événements qui exécute une routine personnalisée chaque fois que cet événement se produit.
Remarque
|
|---|
|
Le contrôle DetailsView déclenche également d'autres événements spécialisés lorsque l'utilisateur clique sur certains boutons (des boutons dont la propriété CommandName a la valeur "Delete", "Insert", "Page" ou "Update"). Lorsque vous utilisez l'un de ces boutons, vous devez envisager d'utiliser l'un des événements spécialisés fournis par le contrôle (tels que ItemDeleted ou ItemDeleting). |
Un objet DetailsViewCommandEventArgs est passé au gestionnaire d'événements. Si le bouton qui a déclenché l'événement a un nom de commande ou une valeur d'argument de commande, vous pouvez utiliser l'objet DetailsViewCommandEventArgs pour déterminer ces valeurs. Pour déterminer le nom et l'argument de commande d'un bouton sur lequel vous avez cliqué, utilisez respectivement les propriétés CommandName et CommandArgument. Vous pouvez également accéder au contrôle DetailsView 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 DetailsViewCommandEventArgs, consultez le constructeur DetailsViewCommandEventArgs.
L'exemple de code suivant montre comment utiliser l'objet DetailsViewCommandEventArgs passé au gestionnaire d'événements de l'événement ItemCommand afin de déterminer le nom de commande du bouton sur lequel l'utilisateur a cliqué. Cet exemple utilise le modèle de codage de fichier unique.
<%@ 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"> void ItemDetailsView_ItemCommand(Object sender, DetailsViewCommandEventArgs e) { // Use the CommandName property to determine which button // was clicked. if (e.CommandName == "Add") { // Add the customer to the customer list. // Get the row that contains the company name. In this // example, the company name is in the second row (index 1) // of the DetailsView control. DetailsViewRow row = ItemDetailsView.Rows[1]; // Get the company's name from the appropriate cell. // In this example, the company name is in the second cell // (index 1) of the row. String name = row.Cells[1].Text; // Create a ListItem object with the company name. ListItem item = new ListItem(name); // Add the ListItem object to the ListBox control, if the // item does not already exist. if (!CustomerListBox.Items.Contains(item)) { CustomerListBox.Items.Add(item); } } } </script> <html xmlns="http://www.w3.org/1999/xhtml" > <head runat="server"> <title>DetailsViewCommandEventArgs Example</title> </head> <body> <form id="form1" runat="server"> <h3>DetailsViewCommandEventArgs Example</h3> <asp:detailsview id="ItemDetailsView" datasourceid="DetailsViewSource" allowpaging="true" autogeneraterows="false" onitemcommand="ItemDetailsView_ItemCommand" runat="server"> <fields> <asp:boundfield datafield="CustomerID" headertext="Customer ID"/> <asp:boundfield datafield="CompanyName" headertext="Company Name"/> <asp:boundfield datafield="Address" headertext="Address"/> <asp:boundfield datafield="City" headertext="City"/> <asp:boundfield datafield="PostalCode" headertext="ZIP Code"/> <asp:boundfield datafield="Country" headertext="Country"/> <asp:buttonfield buttontype="Link" causesvalidation="false" text="Add to List" commandname="Add"/> </fields> </asp:detailsview> <br/><br/> Selected Customers:<br/> <asp:listbox id="CustomerListBox" runat="server"/> <!-- This example uses Microsoft SQL Server and connects --> <!-- to the Northwind sample database. Use an ASP.NET --> <!-- expression to retrieve the connection string value --> <!-- from the Web.config file. --> <asp:sqldatasource id="DetailsViewSource" selectcommand="Select [CustomerID], [CompanyName], [Address], [City], [PostalCode], [Country] From [Customers]" connectionstring= "<%$ ConnectionStrings:NorthWindConnectionString%>" runat="server"/> </form> </body> </html>
L'exemple de code suivant affiche une version de modèle de codage code-behind de l'exemple précédent. Pour que cet exemple fonctionne, vous devez copier le code ci-dessous dans le fichier code-behind associé.
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml" > <head runat="server"> <title>DetailsViewCommandEventArgs Example</title> </head> <body> <form id="Form1" runat="server"> <h3>DetailsViewCommandEventArgs Example</h3> <asp:detailsview id="ItemDetailsView" datasourceid="DetailsViewSource" allowpaging="true" autogeneraterows="false" onitemcommand="ItemDetailsView_ItemCommand" runat="server"> <fields> <asp:boundfield datafield="CustomerID" headertext="Customer ID"/> <asp:boundfield datafield="CompanyName" headertext="Company Name"/> <asp:boundfield datafield="Address" headertext="Address"/> <asp:boundfield datafield="City" headertext="City"/> <asp:boundfield datafield="PostalCode" headertext="ZIP Code"/> <asp:boundfield datafield="Country" headertext="Country"/> <asp:buttonfield buttontype="Link" causesvalidation="false" text="Add to List" commandname="Add"/> </fields> </asp:detailsview> <br/><br/> Selected Customers:<br/> <asp:listbox id="CustomerListBox" runat="server"/> <!-- This example uses Microsoft SQL Server and connects --> <!-- to the Northwind sample database. Use an ASP.NET --> <!-- expression to retrieve the connection string value --> <!-- from the Web.config file. --> <asp:sqldatasource id="DetailsViewSource" selectcommand="Select [CustomerID], [CompanyName], [Address], [City], [PostalCode], [Country] From [Customers]" connectionstring="<%$ ConnectionStrings:NorthWindConnectionString%>" runat="server"/> </form> </body> </html>
L'exemple de code suivant affiche le fichier code-behind de l'exemple précédent.
using System; using System.Data; using System.Configuration; using System.Collections; using System.Web; using System.Web.Security; using System.Web.UI; using System.Web.UI.WebControls; using System.Web.UI.WebControls.WebParts; using System.Web.UI.HtmlControls; public partial class _Default : System.Web.UI.Page { public void ItemDetailsView_ItemCommand(Object sender, DetailsViewCommandEventArgs e) { // Use the CommandName property to determine which button // was clicked. if (e.CommandName == "Add") { // Add the customer to the customer list. // Get the row that contains the company name. In this // example, the company name is in the second row (index 1) // of the DetailsView control. DetailsViewRow row = ItemDetailsView.Rows[1]; // Get the company's name from the appropriate cell. // In this example, the company name is in the second cell // (index 1) of the row. String name = row.Cells[1].Text; // Create a ListItem object with the company name. ListItem item = new ListItem(name); // Add the ListItem object to the ListBox control, if the // item does not already exist. if (!CustomerListBox.Items.Contains(item)) { CustomerListBox.Items.Add(item); } } } }
Windows 7, Windows Vista SP1 ou ultérieur, Windows XP SP3, Windows XP SP2 Édition x64, 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.
Remarque