GridViewCommandEventArgs GridViewCommandEventArgs GridViewCommandEventArgs GridViewCommandEventArgs Class

Definizione

Fornisce dati per l'evento RowCommand.Provides data for the RowCommand event.

public ref class GridViewCommandEventArgs : System::Web::UI::WebControls::CommandEventArgs
public class GridViewCommandEventArgs : System.Web.UI.WebControls.CommandEventArgs
type GridViewCommandEventArgs = class
    inherit CommandEventArgs
Public Class GridViewCommandEventArgs
Inherits CommandEventArgs
Ereditarietà
GridViewCommandEventArgsGridViewCommandEventArgsGridViewCommandEventArgsGridViewCommandEventArgs

Esempi

Nell'esempio seguente viene illustrato come utilizzare l' GridViewCommandEventArgs oggetto passato al metodo di gestione degli eventi per determinare il nome del comando del pulsante che ha generato l'evento.The following example demonstrates how to use the GridViewCommandEventArgs object passed to the event-handling method to determine the command name of the button that raised the event.


<%@ 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 ContactsGridView_RowCommand(Object sender, GridViewCommandEventArgs e)
  {
    // If multiple buttons are used in a GridView control, use the
    // CommandName property to determine which button was clicked.
    if(e.CommandName=="Add")
    {
      // Convert the row index stored in the CommandArgument
      // property to an Integer.
      int index = Convert.ToInt32(e.CommandArgument);

      // Retrieve the row that contains the button clicked 
      // by the user from the Rows collection.
      GridViewRow row = ContactsGridView.Rows[index];

      // Create a new ListItem object for the contact in the row.     
      ListItem item = new ListItem();
      item.Text = Server.HtmlDecode(row.Cells[2].Text) + " " +
        Server.HtmlDecode(row.Cells[3].Text);

      // If the contact is not already in the ListBox, add the ListItem 
      // object to the Items collection of the ListBox control. 
      if (!ContactsListBox.Items.Contains(item))
      {
        ContactsListBox.Items.Add(item);
      }
    }
  }    
</script>

<html xmlns="http://www.w3.org/1999/xhtml" >
  <head runat="server">
    <title>GridView RowCommand Example</title>
</head>
<body>
    <form id="form1" runat="server">

      <h3>GridView RowCommand Example</h3>

      <table width="100%">
        <tr>
          <td style="width:50%">

            <asp:gridview id="ContactsGridView" 
              datasourceid="ContactsSource"
              allowpaging="true" 
              autogeneratecolumns="false"
              onrowcommand="ContactsGridView_RowCommand"
              runat="server">

              <columns>
                <asp:buttonfield buttontype="Link" 
                  commandname="Add" 
                  text="Add"/>
                <asp:boundfield datafield="ContactID" 
                  headertext="Contact ID"/>
                <asp:boundfield datafield="FirstName" 
                  headertext="First Name"/> 
                <asp:boundfield datafield="LastName" 
                  headertext="Last Name"/>
              </columns>

            </asp:gridview>

          </td>

          <td style="vertical-align:top; width:50%">

            Contacts: <br/>
            <asp:listbox id="ContactsListBox"
              runat="server" Height="200px" Width="200px"/>

          </td>
        </tr>
      </table>

      <!-- 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="ContactsSource"
        selectcommand="Select [ContactID], [FirstName], [LastName] From Person.Contact"
        connectionstring="<%$ ConnectionStrings:AdventureWorks_DataConnectionString%>" 
        runat="server"/>

    </form>
  </body>
</html>


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

  Sub ContactsGridView_RowCommand(ByVal sender As Object, ByVal e As GridViewCommandEventArgs)

    ' If multiple buttons are used in a GridView control, use the
    ' CommandName property to determine which button was clicked.
    If e.CommandName = "Add" Then
    
      ' Convert the row index stored in the CommandArgument
      ' property to an Integer.
      Dim index As Integer = Convert.ToInt32(e.CommandArgument)
            
      ' Retrieve the row that contains the button clicked 
      ' by the user from the Rows collection.
      Dim row As GridViewRow = ContactsGridView.Rows(index)
            
      ' Create a new ListItem object for the contact in the row.     
      Dim item As New ListItem()
      item.Text = Server.HtmlDecode(row.Cells(2).Text) & " " & _
        Server.HtmlDecode(row.Cells(3).Text)
            
      ' If the contact is not already in the ListBox, add the ListItem 
      ' object to the Items collection of the ListBox control. 
      If Not ContactsListBox.Items.Contains(item) Then
      
        ContactsListBox.Items.Add(item)
        
      End If
      
    End If
    
  End Sub
    
</script>

<html xmlns="http://www.w3.org/1999/xhtml" >
  <head id="Head1" runat="server">
    <title>GridView RowCommand Example</title>
</head>
<body>
    <form id="form1" runat="server">

      <h3>GridView RowCommand Example</h3>

      <table width="100%">
        <tr>
          <td style="width:50%">

            <asp:gridview id="ContactsGridView" 
              datasourceid="ContactsSource"
              allowpaging="true" 
              autogeneratecolumns="false"
              onrowcommand="ContactsGridView_RowCommand"
              runat="server">

              <columns>
                <asp:buttonfield buttontype="Link" 
                  commandname="Add" 
                  text="Add"/>
                <asp:boundfield datafield="ContactID" 
                  headertext="Contact ID"/>
                <asp:boundfield datafield="FirstName" 
                  headertext="First Name"/> 
                <asp:boundfield datafield="LastName" 
                  headertext="Last Name"/>
              </columns>

            </asp:gridview>

          </td>

          <td style="vertical-align:top; width:50%">

            Contacts: <br/>
            <asp:listbox id="ContactsListBox"
              runat="server" Height="200px" Width="200px"/>

          </td>
        </tr>
      </table>

      <!-- 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="ContactsSource"
        selectcommand="Select [ContactID], [FirstName], [LastName] From Person.Contact"
        connectionstring="<%$ ConnectionStrings:AdventureWorks_DataConnectionString%>" 
        runat="server"/>

    </form>
  </body>
</html>

Commenti

L' RowCommand evento viene generato quando si fa clic su GridView un pulsante all'interno del controllo.The RowCommand event is raised when a button within the GridView control is clicked. In questo modo è possibile fornire un metodo di gestione degli eventi che esegua una routine personalizzata ogni volta che si verifica questo evento.This allows you to provide an event-handling method that performs a custom routine whenever this event occurs.

Nota

Il GridView controllo genera anche altri eventi specializzati quando si fa clic su determinati pulsanti (pulsanti con CommandName la proprietà impostata su "Delete", "Update" e "Page", ad esempio).The GridView control also raises other specialized events when certain buttons are clicked (buttons with the CommandName property set to "Delete", "Update", and "Page" for example). Quando si utilizza uno di questi pulsanti, è consigliabile considerare la gestione di uno degli eventi specializzati forniti dal controllo, ad RowDeleted esempio RowDeletingo.When using one of these buttons, you should consider handling one of the specialized events provided by the control (such as RowDeleted or RowDeleting).

Un GridViewCommandEventArgs oggetto viene passato al metodo di gestione degli eventi, che consente di determinare il nome del comando e l'argomento del comando del pulsante selezionato.A GridViewCommandEventArgs object is passed to the event-handling method, which allows you to determine the command name and command argument of the button clicked. Per determinare il nome del comando e l'argomento del comando CommandName , CommandArgument usare rispettivamente le proprietà e.To determine the command name and command argument, use the CommandName and CommandArgument properties, respectively. È anche possibile accedere al controllo Button che ha generato l'evento usando la CommandSource proprietà.You can also access the button control that raised the event by using the CommandSource property.

Per altre informazioni su come gestire gli eventi, vedere la gestione e generazione di eventi.For more information about how to handle events, see Handling and Raising Events.

Per un elenco dei valori iniziali delle proprietà di un'istanza GridViewCommandEventArgsdi, vedere GridViewCommandEventArgs il costruttore.For a list of initial property values for an instance of GridViewCommandEventArgs, see the GridViewCommandEventArgs constructor.

Costruttori

GridViewCommandEventArgs(GridViewRow, Object, CommandEventArgs) GridViewCommandEventArgs(GridViewRow, Object, CommandEventArgs) GridViewCommandEventArgs(GridViewRow, Object, CommandEventArgs) GridViewCommandEventArgs(GridViewRow, Object, CommandEventArgs)

Inizializza una nuova istanza della classe GridViewCommandEventArgs utilizzando la riga, l'origine del comando e gli argomenti dell'evento specificati.Initializes a new instance of the GridViewCommandEventArgs class using the specified row, source of the command, and event arguments.

GridViewCommandEventArgs(Object, CommandEventArgs) GridViewCommandEventArgs(Object, CommandEventArgs) GridViewCommandEventArgs(Object, CommandEventArgs) GridViewCommandEventArgs(Object, CommandEventArgs)

Inizializza una nuova istanza della classe GridViewCommandEventArgs utilizzando l'origine del comando e gli argomenti dell'evento specificati.Initializes a new instance of the GridViewCommandEventArgs class using the specified source of the command and event arguments.

Proprietà

CommandArgument CommandArgument CommandArgument CommandArgument

Ottiene l'argomento del comando.Gets the argument for the command.

(Inherited from CommandEventArgs)
CommandName CommandName CommandName CommandName

Ottiene il nome del comando.Gets the name of the command.

(Inherited from CommandEventArgs)
CommandSource CommandSource CommandSource CommandSource

Ottiene l'origine del comando.Gets the source of the command.

Handled Handled Handled Handled

Ottiene o imposta un valore che indica se il controllo ha gestito l'evento.Gets or sets a value that indicates whether the control has handled the event.

Metodi

Equals(Object) Equals(Object) Equals(Object) Equals(Object)

Determina se l'oggetto specificato è uguale all'oggetto corrente.Determines whether the specified object is equal to the current object.

(Inherited from Object)
GetHashCode() GetHashCode() GetHashCode() GetHashCode()

Funge da funzione hash predefinita.Serves as the default hash function.

(Inherited from Object)
GetType() GetType() GetType() GetType()

Ottiene l'oggetto Type dell'istanza corrente.Gets the Type of the current instance.

(Inherited from Object)
MemberwiseClone() MemberwiseClone() MemberwiseClone() MemberwiseClone()

Crea una copia superficiale dell'oggetto Object corrente.Creates a shallow copy of the current Object.

(Inherited from Object)
ToString() ToString() ToString() ToString()

Restituisce una stringa che rappresenta l'oggetto corrente.Returns a string that represents the current object.

(Inherited from Object)

Si applica a

Vedi anche