FormViewCommandEventArgs Třída

Definice

Poskytuje data pro událost ItemCommand.

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
Dědičnost
FormViewCommandEventArgs

Příklady

Následující příklad ukazuje, jak použít FormViewCommandEventArgs objekt předaný do metody zpracování událostí pro událost k ItemCommand určení, které tlačítko v rámci FormView ovládacího prvku uživatel kliknul.

Důležité

Tento příklad obsahuje textové pole, které přijímá vstup uživatele, což je potenciální bezpečnostní hrozba. Ve výchozím nastavení ASP.NET webové stránky ověřte, že uživatelský vstup neobsahuje skript nebo elementy HTML. Další informace najdete v tématu Přehled zneužití skriptů.


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

Poznámky

Událost ItemCommand je vyvolána po kliknutí na tlačítko uvnitř FormView ovládacího prvku. To vám umožní poskytnout metodu zpracování událostí, která provádí vlastní rutinu pokaždé, když dojde k této události.

Tlačítka v rámci FormView ovládacího prvku mohou také vyvolat některé integrované funkce ovládacího prvku. Pokud chcete provést jednu z těchto operací, nastavte CommandName vlastnost tlačítka na jednu z hodnot v následující tabulce.

Hodnota CommandName Description
"Zrušit" Zruší operaci úpravy nebo vložení a vrátí FormView ovládací prvek do režimu určeného DefaultMode vlastností . ModeChanged Vyvolá události a ModeChanging .
"Odstranit" Odstraní aktuální záznam. ItemDeleted Vyvolá události a ItemDeleting .
"Upravit" Převede ovládací prvek do FormView režimu úprav. ModeChanged Vyvolá události a ModeChanging .
"Vložit" Vloží aktuální záznam do zdroje dat. ItemInserted Vyvolá události a ItemInserting .
"Nový" Převede ovládací prvek do FormView režimu vložení. ModeChanged Vyvolá události a ModeChanging .
"Stránka" Provede operaci stránkování. CommandArgument Nastavte vlastnost tlačítka na "První", "Poslední", "Další", "Předchozí" nebo číslo stránky a určete typ operace stránkování, která se má provést. PageIndexChanged Vyvolá události a PageIndexChanging .
"Aktualizovat" Aktualizace aktuální záznam ve zdroji dat. ItemUpdated Vyvolá události a ItemUpdating .

ItemCommand I když je událost vyvolána po kliknutí na tlačítko uvedené v předchozí tabulce, doporučuje se pro operaci použít události uvedené v tabulce.

Objekt FormViewCommandEventArgs se předá metodě zpracování událostí, která umožňuje určit název příkazu a argument příkazu pro kliknutí na tlačítko. K určení názvu příkazu a argumentu CommandName příkazu použijte vlastnosti a CommandArgument . Pomocí vlastnosti můžete také získat přístup k ovládacímu prvku tlačítko, který událost CommandSource vyvolal.

Další informace o zpracování událostí najdete v tématu Zpracování a vyvolávání událostí.

Seznam počátečních hodnot vlastností pro instanci FormViewCommandEventArgs třídy naleznete v konstruktoru FormViewCommandEventArgs .

Konstruktory

FormViewCommandEventArgs(Object, CommandEventArgs)

Inicializuje novou instanci FormViewCommandEventArgs třídy .

Vlastnosti

CommandArgument

Získá argument pro příkaz .

(Zděděno od CommandEventArgs)
CommandName

Získá název příkazu.

(Zděděno od CommandEventArgs)
CommandSource

Získá ovládací prvek, který vyvolal událost.

Handled

Získá nebo nastaví hodnotu, která označuje, zda ovládací prvek zpracovává událost.

Metody

Equals(Object)

Určí, zda se zadaný objekt rovná aktuálnímu objektu.

(Zděděno od Object)
GetHashCode()

Slouží jako výchozí hashovací funkce.

(Zděděno od Object)
GetType()

Type Získá z aktuální instance.

(Zděděno od Object)
MemberwiseClone()

Vytvoří mělkou kopii aktuálního Objectsouboru .

(Zděděno od Object)
ToString()

Vrátí řetězec, který představuje aktuální objekt.

(Zděděno od Object)

Platí pro

Viz také