FormViewCommandEventArgs FormViewCommandEventArgs FormViewCommandEventArgs FormViewCommandEventArgs Class

Definition

Stellt Daten für das ItemCommand-Ereignis bereit.Provides data for the ItemCommand event.

public ref class FormViewCommandEventArgs : System::Web::UI::WebControls::CommandEventArgs
public class FormViewCommandEventArgs : System.Web.UI.WebControls.CommandEventArgs
type FormViewCommandEventArgs = class
    inherit CommandEventArgs
Public Class FormViewCommandEventArgs
Inherits CommandEventArgs
Vererbung
FormViewCommandEventArgsFormViewCommandEventArgsFormViewCommandEventArgsFormViewCommandEventArgs

Beispiele

Im folgenden Beispiel wird veranschaulicht, wie das FormViewCommandEventArgs -Objekt verwendet wird, das an die Ereignis Behandlungsmethode für das ItemCommand -Ereignis über FormView mittelt wird, um zu bestimmen, auf welche Schaltfläche in einem-Steuerelement vom BenutzerThe following example demonstrates how to use the FormViewCommandEventArgs object passed to the event-handling method for the ItemCommand event to determine which button within a FormView control was clicked by the user.

Wichtig

Dieses Beispiel umfasst ein Textfeld, das Benutzereingaben akzeptiert, die ein potenzielles Sicherheitsrisiko darstellen.This example has a text box that accepts user input, which is a potential security threat. Standardmäßig stellen ASP.NET-Webseiten sicher, dass Benutzereingaben keine Skript- oder HTML-Elemente enthalten.By default, ASP.NET Web pages validate that user input does not include script or HTML elements. Weitere Informationen finden Sie unter Übersicht über Skriptangriffe.For more information, see Script Exploits Overview.


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


<%@ 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 ProductFormView_ItemCommand(ByVal sender As Object, ByVal e As FormViewCommandEventArgs)

    ' 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" Then

      ' Add the product to the ListBox control. 
      
      ' Use the Row property to retrieve the data row.
      Dim row As FormViewRow = ProductFormView.Row
      
      ' Retrieve the ProductNameLabel control from
      ' the data row.
      Dim productNameLabel As Label = CType(row.FindControl("ProductNameLabel"), Label)

      ' Retrieve the QuantityTextBox control from
      ' the data row.
      Dim quantityTextBox As TextBox = CType(row.FindControl("QuantityTextBox"), TextBox)

      If productNameLabel IsNot Nothing And quantityTextBox IsNot Nothing Then
          
        ' Get the product name from the ProductNameLabel control.
        Dim name As String = productNameLabel.Text
        
        ' Get the quantity from the QuantityTextBox control.
        Dim quantity As String = quantityTextBox.Text

        ' Create the text to display in the ListBox control.
        Dim description As String = name & " - " & quantity & " Qty"

        ' Create a ListItem object using the description and
        ' product name.
        Dim item As 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.
        Dim addButton As Button = CType(e.CommandSource, Button)
        addButton.Enabled = False
        
      End If

    End If

  End Sub

  Sub ProductFormView_DataBound(ByVal sender As Object, ByVal e As EventArgs)
    
    ' 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.
    Dim row As FormViewRow = ProductFormView.Row

    ' Retrieve the Add button from the data row.
    Dim addButton As Button = CType(row.FindControl("AddButton"), Button)

    ' Retrieve the ProductNameLabel control from
    ' data row.
    Dim productNameLabel As Label = CType(row.FindControl("ProductNameLabel"), Label)

    If addButton IsNot Nothing And productNameLabel IsNot Nothing Then
    
      ' Get the product name from the ProductNameLabel 
      ' control.
      Dim name As String = productNameLabel.Text

      ' Use the FindByValue method to determine whether
      ' the ListBox control already contains an entry for
      ' the item.
      Dim item As ListItem = ProductListBox.Items.FindByValue(name)

      ' Disable the Add button if the ListBox control
      ' already contains the item.
      If item IsNot Nothing Then
      
        addButton.Enabled = False
      
      Else
      
        addButton.Enabled = True
        
      End If
      
    End If

  End Sub
    
</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>

Hinweise

Das ItemCommand -Ereignis wird ausgelöst, wenn FormView im-Steuerelement auf eine Schaltfläche geklickt wird.The ItemCommand event is raised when a button within the FormView control is clicked. Dies ermöglicht es Ihnen, eine Ereignis Behandlungsmethode bereitzustellen, die immer dann eine benutzerdefinierte Routine ausführt, wenn dieses Ereignis auftritt.This allows you to provide an event-handling method that performs a custom routine whenever this event occurs.

Schaltflächen in FormView einem-Steuerelement können auch einige der integrierten Funktionen des-Steuer Elements aufrufen.Buttons within a FormView control can also invoke some of the built-in functionality of the control. Um einen dieser Vorgänge auszuführen, legen Sie die CommandName -Eigenschaft einer Schaltfläche auf einen der Werte in der folgenden Tabelle fest.To perform one of these operations, set the CommandName property of a button to one of the values in the following table.

CommandName-WertCommandName value BeschreibungDescription
Jederzeit"Cancel" Bricht einen Bearbeitungs-oder Einfügevorgang ab FormView und gibt das Steuerelement in den DefaultMode von der-Eigenschaft angegebenen Modus zurück.Cancels an edit or insert operation and returns the FormView control to the mode specified by the DefaultMode property. Löst die ModeChanged Ereignisse ModeChanging und aus.Raises the ModeChanged and ModeChanging events.
"Löschen""Delete" Löscht den aktuellen Datensatz.Deletes the current record. Löst die ItemDeleted Ereignisse ItemDeleting und aus.Raises the ItemDeleted and ItemDeleting events.
Bearbeiten"Edit" Versetzt das FormView Steuerelement in den Bearbeitungsmodus.Puts the FormView control in edit mode. Löst die ModeChanged Ereignisse ModeChanging und aus.Raises the ModeChanged and ModeChanging events.
Setze"Insert" Fügt den aktuellen Datensatz in der Datenquelle ein.Inserts the current record in the data source. Löst die ItemInserted Ereignisse ItemInserting und aus.Raises the ItemInserted and ItemInserting events.
Neubau"New" Versetzt das FormView Steuerelement in den Einfügemodus.Puts the FormView control in insert mode. Löst die ModeChanged Ereignisse ModeChanging und aus.Raises the ModeChanged and ModeChanging events.
S"Page" Führt einen Pagingvorgang aus.Performs a paging operation. Legen Sie CommandArgument die-Eigenschaft der Schaltfläche auf "First", "Last", "Next", "Prev" oder eine Seitenzahl fest, um den Typ des auszuführenden Paging-Vorgangs anzugeben.Set the CommandArgument property of the button to "First", "Last", "Next", "Prev", or a page number to specify the type of paging operation to perform. Löst die PageIndexChanged Ereignisse PageIndexChanging und aus.Raises the PageIndexChanged and PageIndexChanging events.
Alisierungs"Update" Aktualisiert den aktuellen Datensatz in der Datenquelle.Updates the current record in the data source. Löst die ItemUpdated Ereignisse ItemUpdating und aus.Raises the ItemUpdated and ItemUpdating events.

Obwohl das ItemCommand -Ereignis ausgelöst wird, wenn auf eine in der vorherigen Tabelle aufgeführte Schaltfläche geklickt wird, wird empfohlen, die in der Tabelle für den Vorgang aufgelisteten Ereignisse zu verwenden.Although the ItemCommand event is raised when a button listed in the previous table is clicked, it is recommended that you use the events listed in the table for the operation.

Ein FormViewCommandEventArgs -Objekt wird an die Ereignis Behandlungsmethode übermittelt, mit der Sie den Befehlsnamen und das Befehls Argument der Schaltfläche, auf die geklickt wurde, bestimmen können.A FormViewCommandEventArgs object is passed to the event-handling method, which allows you to determine the command name and command argument of the button clicked. Name des Befehls und Befehlsargument ermitteln Sie mithilfe der CommandName und CommandArgument Eigenschaften bzw.To determine the command name and command argument, use the CommandName and CommandArgument properties, respectively. Mithilfe der CommandSource -Eigenschaft können Sie auch auf das Schaltflächen-Steuerelement zugreifen, das das Ereignis ausgelöst hat.You can also access the button control that raised the event by using the CommandSource property.

Weitere Informationen zum Behandeln von Ereignissen finden Sie unter behandeln und Auslösen von Ereignissen.For more information about how to handle events, see Handling and Raising Events.

Eine Liste der anfänglichen Eigenschaftenwerte für eine Instanz der FormViewCommandEventArgs-Klasse finden Sie im FormViewCommandEventArgs-Konstruktor.For a list of initial property values for an instance of the FormViewCommandEventArgs class, see the FormViewCommandEventArgs constructor.

Konstruktoren

FormViewCommandEventArgs(Object, CommandEventArgs) FormViewCommandEventArgs(Object, CommandEventArgs) FormViewCommandEventArgs(Object, CommandEventArgs) FormViewCommandEventArgs(Object, CommandEventArgs)

Initialisiert eine neue Instanz der FormViewCommandEventArgs-Klasse.Initializes a new instance of the FormViewCommandEventArgs class.

Eigenschaften

CommandArgument CommandArgument CommandArgument CommandArgument

Ruft das Argument für den Befehl ab.Gets the argument for the command.

(Inherited from CommandEventArgs)
CommandName CommandName CommandName CommandName

Ruft den Namen des Befehls ab.Gets the name of the command.

(Inherited from CommandEventArgs)
CommandSource CommandSource CommandSource CommandSource

Ruft das Steuerelement ab, durch das das Ereignis ausgelöst wurde.Gets the control that raised the event.

Handled Handled Handled Handled

Ruft einen Wert ab, der angibt, ob das Steuerelement das Ereignis behandelt hat, oder legt diesen fest.Gets or sets a value that indicates whether the control has handled the event.

Methoden

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

Bestimmt, ob das angegebene Objekt mit dem aktuellen Objekt identisch ist.Determines whether the specified object is equal to the current object.

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

Fungiert als Standardhashfunktion.Serves as the default hash function.

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

Ruft den Type der aktuellen Instanz ab.Gets the Type of the current instance.

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

Erstellt eine flache Kopie des aktuellen Object.Creates a shallow copy of the current Object.

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

Gibt eine Zeichenfolge zurück, die das aktuelle Objekt darstellt.Returns a string that represents the current object.

(Inherited from Object)

Gilt für:

Siehe auch