FormViewInsertEventArgs Classe

Définition

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

public ref class FormViewInsertEventArgs : System::ComponentModel::CancelEventArgs
public class FormViewInsertEventArgs : System.ComponentModel.CancelEventArgs
type FormViewInsertEventArgs = class
    inherit CancelEventArgs
Public Class FormViewInsertEventArgs
Inherits CancelEventArgs
Héritage
FormViewInsertEventArgs

Exemples

L’exemple suivant montre comment utiliser l’objet FormViewInsertEventArgs passé à la méthode de gestion des événements pour l’événement ItemInserting afin d’annuler une opération d’insertion lorsque l’utilisateur laisse un champ vide.

Important

Cet exemple contient une zone de texte qui accepte l’entrée utilisateur, qui est une menace de sécurité potentielle. Par défaut, les pages web ASP.NET vérifient que l’entrée d’utilisateur n’inclut pas de script ou 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 EmployeeFormView_ItemInserting(Object sender, FormViewInsertEventArgs e)
  {

    MessageLabel.Text = "";

    // Iterate through the items in the Values collection
    // and verify that the user entered a value for each 
    // text box displayed in the insert item template. Cancel
    // the insert operation if the user left a text box empty.
    foreach (DictionaryEntry entry in e.Values)
    {
      if (entry.Value.Equals(""))
      {
        // Use the Cancel property to cancel the 
        // insert operation.
        e.Cancel = true;

        MessageLabel.Text += "Please enter a value for the " +
          entry.Key.ToString() + " field.<br/>";

      }
    }
  }

  void EmployeeFormView_ModeChanged(Object sender, EventArgs e)
  {
    // Clear the MessageLabel Label control when the FormView
    // control changes modes.
    MessageLabel.Text = "";
  }

</script>

<html xmlns="http://www.w3.org/1999/xhtml" >
  <head runat="server">
    <title>FormViewInsertEventArgs Example</title>
</head>
<body>
    <form id="form1" runat="server">
        
      <h3>FormViewInsertEventArgs Example</h3>
                       
      <asp:formview id="EmployeeFormView"
        datasourceid="EmployeeSource"
        allowpaging="true"
        datakeynames="EmployeeID"
        emptydatatext="No employees found."
        oniteminserting="EmployeeFormView_ItemInserting"
        onmodechanged="EmployeeFormView_ModeChanged"
        runat="server">

        <itemtemplate>
          <table>
            <tr>
              <td rowspan="5">
                <asp:image id="CompanyLogoImage"
                  imageurl="~/Images/Logo.jpg"
                  alternatetext="Company logo"
                  runat="server"/>
              </td>
              <td colspan="2">
                    
              </td>
            </tr>
            <tr>
              <td>
                <b>Name:</b>
              </td>
              <td>
                <%# Eval("FirstName") %> <%# Eval("LastName") %>
              </td>
            </tr>
            <tr>
              <td>
                <b>Title:</b>
              </td>
              <td>
                <%# Eval("Title") %>
              </td>
            </tr>
            <tr>
              <td colspan="2">
                <asp:linkbutton id="NewButton"
                  text="New"
                  commandname="New"
                  runat="server"/> 
              </td>
            </tr>
          </table>       
        </itemtemplate>
        <insertitemtemplate>
          <table>
            <tr>
              <td rowspan="4">
                <asp:image id="CompanyLogoEditImage"
                  imageurl="~/Images/Logo.jpg"
                  alternatetext="Company logo"
                  runat="server"/>
              </td>
              <td colspan="2">
                    
              </td>
            </tr>
            <tr>
              <td>
                <b><asp:Label
                  runat="server" 
                  AssociatedControlID="FirstNameInsertTextBox" 
                  Text="Name" />:</b>
              </td>
              <td>
                <asp:textbox id="FirstNameInsertTextBox"
                  text='<%# Bind("FirstName") %>'
                  runat="server"/>
                <asp:textbox id="LastNameInsertTextBox"
                  text='<%# Bind("LastName") %>'
                  runat="server"/>
              </td>
            </tr>
            <tr>
              <td>
                <b><asp:Label
                  runat="server" 
                  AssociatedControlID="TitleInsertTextBox" 
                  Text="Title" />:</b>
              </td>
              <td>
                <asp:textbox id="TitleInsertTextBox"
                  text='<%# Bind("Title") %>'
                  runat="server"/> 
              </td>
            </tr>
            <tr>
              <td colspan="2">
                <asp:linkbutton id="InsertButton"
                  text="Insert"
                  commandname="Insert"
                  runat="server"/>
                <asp:linkbutton id="CancelButton"
                  text="Cancel"
                  commandname="Cancel"
                  runat="server"/> 
              </td>
            </tr>
          </table>       
        </insertitemtemplate> 
                  
      </asp:formview>
      
      <br/><br/>
      
      <asp:label id="MessageLabel"
        forecolor="Red"
        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="EmployeeSource"
        selectcommand="Select [EmployeeID], [LastName], [FirstName], [Title], [PhotoPath] From [Employees]"
        insertcommand="Insert Into [Employees] ([LastName], [FirstName], [Title]) VALUES (@LastName, @FirstName, @Title)"
        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 EmployeeFormView_ItemInserting(ByVal sender As Object, ByVal e As FormViewInsertEventArgs)

    MessageLabel.Text = ""

    ' Iterate through the items in the Values collection
    ' and verify that the user entered a value for each 
    ' text box displayed in the insert item template. Cancel
    ' the insert operation if the user left a text box empty.
    
    ' In Visual Basic, the DictionaryItem objects contained in 
    ' the Values collection must be copied to an array before
    ' you can iterate through the collection.
    Dim itemArray(e.Values.Count - 1) As DictionaryEntry
    e.Values.CopyTo(itemArray, 0)
    
    Dim entry As DictionaryEntry
    For Each entry In itemArray
    
      If entry.Value.Equals("") Then
      
        ' Use the Cancel property to cancel the 
        ' insert operation.
        e.Cancel = True

        MessageLabel.Text &= "Please enter a value for the " & _
          entry.Key.ToString() & " field.<br/>"
      
      End If
      
    Next
    
  End Sub

  Sub EmployeeFormView_ModeChanged(ByVal sender As Object, ByVal e As EventArgs)
  
    ' Clear the MessageLabel Label control when the FormView
    ' control changes modes.
    MessageLabel.Text = ""
  
  End Sub

</script>

<html xmlns="http://www.w3.org/1999/xhtml" >
  <head runat="server">
    <title>FormViewInsertEventArgs Example</title>
</head>
<body>
    <form id="form1" runat="server">
        
      <h3>FormViewInsertEventArgs Example</h3>
                       
      <asp:formview id="EmployeeFormView"
        datasourceid="EmployeeSource"
        allowpaging="true"
        datakeynames="EmployeeID"
        emptydatatext="No employees found."
        oniteminserting="EmployeeFormView_ItemInserting"
        onmodechanged="EmployeeFormView_ModeChanged"
        runat="server">

        <itemtemplate>
          <table>
            <tr>
              <td rowspan="5">
                <asp:image id="CompanyLogoImage"
                  imageurl="~/Images/Logo.jpg"
                  alternatetext="Company logo"
                  runat="server"/>
              </td>
              <td colspan="2">
                    
              </td>
            </tr>
            <tr>
              <td>
                <b>Name:</b>
              </td>
              <td>
                <%# Eval("FirstName") %> <%# Eval("LastName") %>
              </td>
            </tr>
            <tr>
              <td>
                <b>Title:</b>
              </td>
              <td>
                <%# Eval("Title") %>
              </td>
            </tr>
            <tr>
              <td colspan="2">
                <asp:linkbutton id="NewButton"
                  text="New"
                  commandname="New"
                  runat="server"/> 
              </td>
            </tr>
          </table>       
        </itemtemplate>
        <insertitemtemplate>
          <table>
            <tr>
              <td rowspan="4">
                <asp:image id="CompanyLogoEditImage"
                  imageurl="~/Images/Logo.jpg"
                  alternatetext="Company logo"
                  runat="server"/>
              </td>
              <td colspan="2">
                    
              </td>
            </tr>
            <tr>
              <td>
                <b><asp:Label
                  runat="server" 
                  AssociatedControlID="FirstNameInsertTextBox" 
                  Text="Name" />:</b>
              </td>
              <td>
                <asp:textbox id="FirstNameInsertTextBox"
                  text='<%# Bind("FirstName") %>'
                  runat="server"/>
                <asp:textbox id="LastNameInsertTextBox"
                  text='<%# Bind("LastName") %>'
                  runat="server"/>
              </td>
            </tr>
            <tr>
              <td>
                <b><asp:Label
                  runat="server" 
                  AssociatedControlID="TitleInsertTextBox" 
                  Text="Title" />:</b>
              </td>
              <td>
                <asp:textbox id="TitleInsertTextBox"
                  text='<%# Bind("Title") %>'
                  runat="server"/> 
              </td>
            </tr>
            <tr>
              <td colspan="2">
                <asp:linkbutton id="InsertButton"
                  text="Insert"
                  commandname="Insert"
                  runat="server"/>
                <asp:linkbutton id="CancelButton"
                  text="Cancel"
                  commandname="Cancel"
                  runat="server"/> 
              </td>
            </tr>
          </table>       
        </insertitemtemplate> 
                  
      </asp:formview>
      
      <br/><br/>
      
      <asp:label id="MessageLabel"
        forecolor="Red"
        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="EmployeeSource"
        selectcommand="Select [EmployeeID], [LastName], [FirstName], [Title], [PhotoPath] From [Employees]"
        insertcommand="Insert Into [Employees] ([LastName], [FirstName], [Title]) VALUES (@LastName, @FirstName, @Title)"
        connectionstring="<%$ ConnectionStrings:NorthWindConnectionString%>" 
        runat="server"/>
            
    </form>
  </body>
</html>

Remarques

Le FormView contrôle déclenche l’événement ItemInserting lorsqu’un bouton Insérer (un bouton dont CommandName la propriété est définie sur « Insérer ») dans le contrôle est cliqué, mais avant que le FormView contrôle insère l’enregistrement. Cela vous permet de fournir une méthode de gestion des événements qui effectue une routine personnalisée, telle que l’encodage HTML ou la validation des valeurs d’un enregistrement avant de l’insérer dans la source de données, chaque fois que cet événement se produit.

Un FormViewInsertEventArgs objet est passé à la méthode de gestion des événements, ce qui vous permet de déterminer la valeur d’un argument de commande facultatif envoyé au FormView contrôle et d’indiquer que l’opération d’insertion doit être annulée. Pour déterminer la valeur de l’argument de commande, utilisez la CommandArgument propriété . Pour annuler l’opération d’insertion, définissez la propriété sur Canceltrue. Vous pouvez également lire ou modifier les valeurs de champ du nouvel enregistrement à l’aide de la Values propriété .

Pour plus d’informations sur la façon de gérer les événements, consultez gestion et déclenchement d’événements.

Pour obtenir la liste des valeurs de propriété initiales d'une instance de la classe FormViewInsertEventArgs, consultez le constructeur FormViewInsertEventArgs.

Constructeurs

FormViewInsertEventArgs(Object)

Initialise une nouvelle instance de la classe FormViewInsertEventArgs.

Propriétés

Cancel

Obtient ou définit une valeur indiquant si l'événement doit être annulé.

(Hérité de CancelEventArgs)
CommandArgument

Obtient l'argument de commande pour l'opération d'insertion passée au contrôle FormView.

Values

Obtient un dictionnaire qui contient les paires nom/valeur des champs de l'enregistrement à insérer.

Méthodes

Equals(Object)

Détermine si l'objet spécifié est égal à l'objet actuel.

(Hérité de Object)
GetHashCode()

Fait office de fonction de hachage par défaut.

(Hérité de Object)
GetType()

Obtient le Type de l'instance actuelle.

(Hérité de Object)
MemberwiseClone()

Crée une copie superficielle du Object actuel.

(Hérité de Object)
ToString()

Retourne une chaîne qui représente l'objet actuel.

(Hérité de Object)

S’applique à

Voir aussi