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
|
FormViewCommandEventArgs, classe
Fournit des données pour l'événement ItemCommand.
System.EventArgs
System.Web.UI.WebControls.CommandEventArgs
System.Web.UI.WebControls.FormViewCommandEventArgs
Assembly : System.Web (dans System.Web.dll)
Le type FormViewCommandEventArgs expose les membres suivants.
| Nom | Description | |
|---|---|---|
|
FormViewCommandEventArgs | Initialise une nouvelle instance de la classe FormViewCommandEventArgs. |
| 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 le contrôle qui a déclenché l'événement. |
| 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.) |
L'événement ItemCommand est déclenché lorsque l'on clique sur un bouton du contrôle FormView. 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.
Les boutons d'un contrôle FormView peuvent aussi appeler quelques-unes des fonctionnalités intégrées du contrôle. Pour exécuter l'une de ces opérations, définissez la propriété CommandName d'un bouton avec l'une des valeurs du tableau suivant.
|
Valeur de CommandName |
Description |
|---|---|
|
"Cancel" |
Annule une opération de modification ou d'insertion et retourne le contrôle FormView au mode spécifié par la propriété DefaultMode. Déclenche les événements ModeChanged et ModeChanging. |
|
"Delete" |
Supprime l'enregistrement actif. Déclenche les événements ItemDeleted et ItemDeleting. |
|
"Edit" |
Place le contrôle FormView en mode édition. Déclenche les événements ModeChanged et ModeChanging. |
|
"Insert" |
Insère l'enregistrement actif dans la source de données. Déclenche les événements ItemInserted et ItemInserting. |
|
"New" |
Place le contrôle FormView en mode insertion. Déclenche les événements ModeChanged et ModeChanging. |
|
"Page" |
Exécute une opération de pagination. Affectez à la propriété CommandArgument du bouton la valeur "First", "Last", "Next", "Prev", ou un numéro de page, pour spécifier le type d'opération de pagination à effectuer. Déclenche les événements PageIndexChanged et PageIndexChanging. |
|
"Update" |
Met à jour l'enregistrement en cours dans la source de données. Déclenche les événements ItemUpdated et ItemUpdating. |
Bien que l'événement ItemCommand soit déclenché en cas de clic sur un bouton répertorié dans le tableau précédent, il est recommandé d'utiliser les événements répertoriés dans le tableau pour l'opération.
Un objet FormViewCommandEventArgs 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 associés au bouton sur lequel vous cliquez. Pour déterminer le nom de la commande et son argument, utilisez respectivement les propriétés CommandName et CommandArgument. Vous pouvez également accéder au contrôle bouton qui a déclenché l'événement en utilisant la propriété CommandSource.
Pour plus d'informations sur la gestion d'événements, consultez Consommation d'événements.
Pour obtenir la liste des valeurs initiales des propriétés d'une instance de la classe FormViewCommandEventArgs, consultez le constructeur FormViewCommandEventArgs.
L'exemple suivant montre comment utiliser l'objet FormViewCommandEventArgs passé à la méthode de gestion d'événements pour l'événement ItemCommand pour déterminer sur quel bouton d'un contrôle FormView l'utilisateur a cliqué.
Note de sécurité
|
|---|
|
Cet exemple a une zone de texte qui accepte l'entrée d'utilisateur, ce qui constitue une menace éventuelle pour la sécurité. Par défaut, les pages Web ASP.NET vérifient que l'entrée d'utilisateur n'inclut pas de script ni d'éléments HTML. Pour plus d'informations, consultez Vue d'ensemble des attaques de script. |
<%@ 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 ProductFormView_ItemCommand(Object sender, FormViewCommandEventArgs e) { // The ItemCommand event is raised when any button within // the FormView control is clicked. Use the CommandName property // to determine which button was clicked. if (e.CommandName == "Add") { // Add the product to the ListBox control. // Use the Row property to retrieve the data row. FormViewRow row = ProductFormView.Row; // Retrieve the ProductNameLabel control from // the data row. Label productNameLabel = (Label)row.FindControl("ProductNameLabel"); // Retrieve the QuantityTextBox control from // the data row. TextBox quantityTextBox = (TextBox)row.FindControl("QuantityTextBox"); if (productNameLabel != null && quantityTextBox != null) { // Get the product name from the ProductNameLabel control. string name = productNameLabel.Text; // Get the quantity from the QuantityTextBox control. string quantity = quantityTextBox.Text; // Create the text to display in the ListBox control. string description = name + " - " + quantity + " Qty"; // Create a ListItem object using the description and // product name. ListItem item = new ListItem(description, name); // Add the ListItem object to the ListBox. ProductListBox.Items.Add(item); // Use the CommandSource property to retrieve // the Add button. Disable the button after // the user adds the currently displayed employee // name to the ListBox control. Button addButton = (Button)e.CommandSource; addButton.Enabled = false; } } } void ProductFormView_DataBound(Object sender, EventArgs e) { // To prevent the user from adding duplicate items, // disable the Add button if the item being bound to the // FormView control is already in the ListBox control. // Use the Row property to retrieve the data row. FormViewRow row = ProductFormView.Row; // Retrieve the Add button from the data row. Button addButton = (Button)row.FindControl("AddButton"); // Retrieve the ProductNameLabel control from // data row. Label productNameLabel = (Label)row.FindControl("ProductNameLabel"); if (addButton != null && productNameLabel != null) { // Get the product name from the ProductNameLabel // control. string name = productNameLabel.Text; // Use the FindByValue method to determine whether // the ListBox control already contains an entry for // the item. ListItem item = ProductListBox.Items.FindByValue(name); // Disable the Add button if the ListBox control // already contains the item. if (item != null) { addButton.Enabled = false; } else { addButton.Enabled = true; } } } </script> <html xmlns="http://www.w3.org/1999/xhtml" > <head runat="server"> <title>FormViewCommandEventArgs Example</title> </head> <body> <form id="form1" runat="server"> <h3>FormViewCommandEventArgs Example</h3> <asp:formview id="ProductFormView" datasourceid="ProductSource" allowpaging="true" datakeynames="ProductID" onitemcommand="ProductFormView_ItemCommand" ondatabound="ProductFormView_DataBound" runat="server"> <itemtemplate> <table> <tr> <td style="width:400px"> <b>Description:</b> <asp:label id="ProductNameLabel" text='<%# Eval("ProductName") %>' runat='server'/> <br/> <b>Price:</b> <asp:label id="PriceLabel" text='<%# Eval("UnitPrice", "{0:c}") %>' runat='server'/> <br/> <asp:textbox id="QuantityTextBox" width="50px" maxlength="3" runat="server"/> Qty </td> </tr> <tr> <td> <asp:requiredfieldvalidator ID="QuantityRequiredValidator" controltovalidate="QuantityTextBox" text="Please enter a quantity." display="Static" runat="server"/> <br/> <asp:CompareValidator id="QuantityCompareValidator" controltovalidate="QuantityTextBox" text="Please enter an integer value." display="Static" type="Integer" operator="DataTypeCheck" runat="server"/> </td> </tr> <tr> <td colspan="2"> <asp:button id="AddButton" text="Add" commandname="Add" runat="server"/> </td> </tr> </table> </itemtemplate> </asp:formview> <br/><br/><hr/> Items:<br/> <asp:listbox id="ProductListBox" 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="ProductSource" selectcommand="Select [ProductID], [ProductName], [UnitPrice] From [Products]" connectionstring="<%$ ConnectionStrings:NorthWindConnectionString%>" runat="server"/> </form> </body> </html>
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.
Note de sécurité