Cet article a fait l’objet d’une traduction automatique. Pour afficher l’article en anglais, activez la case d’option Anglais. Vous pouvez également afficher le texte anglais dans une fenêtre contextuelle en faisant glisser le pointeur de la souris sur le texte traduit.
Traduction
Anglais

DetailsViewCommandEventArgs classe

 

Date de publication : novembre 2016

Fournit des données pour l'événement ItemCommand.

Espace de noms:   System.Web.UI.WebControls
Assembly:  System.Web (dans System.Web.dll)

System.Object
  System.EventArgs
    System.Web.UI.WebControls.CommandEventArgs
      System.Web.UI.WebControls.DetailsViewCommandEventArgs

public class DetailsViewCommandEventArgs : CommandEventArgs

NomDescription
System_CAPS_pubmethodDetailsViewCommandEventArgs(Object, CommandEventArgs)

Initialise une nouvelle instance de la classe DetailsViewCommandEventArgs.

NomDescription
System_CAPS_pubpropertyCommandArgument

Obtient l’argument de la commande.(Hérité de CommandEventArgs.)

System_CAPS_pubpropertyCommandName

Obtient le nom de la commande.(Hérité de CommandEventArgs.)

System_CAPS_pubpropertyCommandSource

Obtient la source de la commande.

System_CAPS_pubpropertyHandled

Obtient ou définit une valeur qui indique si le contrôle a géré l’événement.

NomDescription
System_CAPS_pubmethodEquals(Object)

Détermine si l'objet spécifié est identique à l'objet actuel.(Hérité de Object.)

System_CAPS_protmethodFinalize()

Autorise un objet à tenter de libérer des ressources et d'exécuter d'autres opérations de nettoyage avant qu'il ne soit récupéré par l'opération garbage collection. (Hérité de Object.)

System_CAPS_pubmethodGetHashCode()

Fait office de fonction de hachage par défaut.(Hérité de Object.)

System_CAPS_pubmethodGetType()

Obtient le Type de l'instance actuelle.(Hérité de Object.)

System_CAPS_protmethodMemberwiseClone()

Crée une copie superficielle du Object actuel.(Hérité de Object.)

System_CAPS_pubmethodToString()

Retourne une chaîne qui représente l'objet actuel.(Hérité de Object.)

The T:System.Web.UI.WebControls.DetailsView control raises the E:System.Web.UI.WebControls.DetailsView.ItemCommand event when a button within a T:System.Web.UI.WebControls.ButtonField, T:System.Web.UI.WebControls.CommandField, or T:System.Web.UI.WebControls.TemplateField row field is clicked. This allows you to provide an event handler that performs a custom routine whenever this event occurs.

System_CAPS_noteRemarque

The T:System.Web.UI.WebControls.DetailsView control also raises other specialized events when certain buttons are clicked (buttons with the CommandName property set to "Delete", "Insert", "Page", or "Update"). When using one of these buttons, you should consider using one of the specialized events provided by the control (such as E:System.Web.UI.WebControls.DetailsView.ItemDeleted or E:System.Web.UI.WebControls.DetailsView.ItemDeleting).

A T:System.Web.UI.WebControls.DetailsViewCommandEventArgs object is passed to the event handler. If the button that raised the event has a command name or command argument value, you can use the T:System.Web.UI.WebControls.DetailsViewCommandEventArgs object to determine these values. To determine the command name and command argument of the button clicked, use the P:System.Web.UI.WebControls.CommandEventArgs.CommandName and P:System.Web.UI.WebControls.CommandEventArgs.CommandArgument properties, respectively. You can also access the T:System.Web.UI.WebControls.DetailsView control that raised the event by using the P:System.Web.UI.WebControls.DetailsViewCommandEventArgs.CommandSource property.

For a list of initial property values for an instance of the T:System.Web.UI.WebControls.DetailsViewCommandEventArgs class, see the M:System.Web.UI.WebControls.DetailsViewCommandEventArgs.#ctor(System.Object,System.Web.UI.WebControls.CommandEventArgs) constructor.

The following code example demonstrates how to use the T:System.Web.UI.WebControls.DetailsViewCommandEventArgs object passed to the event handler for the E:System.Web.UI.WebControls.DetailsView.ItemCommand event to determine the command name of the button clicked by the user. This example uses the single-file coding model.


<%@ 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>

The following code example shows a code-behind coding model version of the previous example. For this example to work, you must copy the code below into the associated code-behind file.

<%@ 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>

The following code example shows the code-behind file for the previous example.

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);
			}

		}

	}
}

.NET Framework
Disponible depuis 2.0

Tous les membres statiques publics ( Shared en Visual Basic) de ce type sont thread-safe. Les membres d’instance n’ont pas la garantie d’être thread-safe.

Retour au début
Afficher: