ListView.ItemUpdated Événement

Définition

Se produit lorsqu'une opération de mise à jour est demandée, après que le contrôle ListView ait mis à jour l'élément.

public:
 event EventHandler<System::Web::UI::WebControls::ListViewUpdatedEventArgs ^> ^ ItemUpdated;
public event EventHandler<System.Web.UI.WebControls.ListViewUpdatedEventArgs> ItemUpdated;
member this.ItemUpdated : EventHandler<System.Web.UI.WebControls.ListViewUpdatedEventArgs> 
Public Custom Event ItemUpdated As EventHandler(Of ListViewUpdatedEventArgs) 

Type d'événement

Exemples

L’exemple suivant montre comment ajouter un gestionnaire d’événements pour l’événement ItemUpdated .

Important

Cet exemple comprend une zone de texte qui accepte une entrée d'utilisateur, ce qui constitue une menace potentielle pour la sécurité. 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 Page_Load()
    {
        Message.Text = String.Empty;
    }
        
    //<Snippet2>
    void ContactsListView_ItemUpdated(Object sender, ListViewUpdatedEventArgs e)
    {
        if (e.Exception != null)
        {
            if (e.AffectedRows == 0)
            {
                e.KeepInEditMode = true;
                Message.Text = "An exception occurred updating the contact. " +
                                    "Please verify your values and try again.";
            }
            else
                Message.Text = "An exception occurred updating the contact. " +
                                    "Please verify the values in the recently updated item.";

            e.ExceptionHandled = true;
        }
    }
   //</Snippet2>
    
</script>

<html xmlns="http://www.w3.org/1999/xhtml" >
  <head id="Head1" runat="server">
    <title>ListView.ItemUpdated Example</title>
</head>
<body>
    <form id="form1" runat="server">
        
      <h3>ListView.ItemUpdated Example</h3>
            
      <asp:Label ID="Message"
        ForeColor="Red"          
        runat="server"/>
      <br/>

      <asp:ListView ID="ContactsListView" 
        DataSourceID="ContactsDataSource" 
        DataKeyNames="ContactID"
        OnItemUpdated="ContactsListView_ItemUpdated"  
        runat="server">
        <LayoutTemplate>
          <table cellpadding="2" border="1" runat="server" id="tblContacts" width="640px">
            <tr runat="server" id="itemPlaceholder" />
          </table>
          <asp:DataPager runat="server" ID="PeopleDataPager" PageSize="12">
             <Fields>
               <asp:NextPreviousPagerField 
                ShowFirstPageButton="True" ShowLastPageButton="True"
                    FirstPageText="|&lt;&lt; " LastPageText=" &gt;&gt;|"
                    NextPageText=" &gt; " PreviousPageText=" &lt; " />
             </Fields>
           </asp:DataPager>
         </LayoutTemplate>
         <ItemTemplate>
            <tr runat="server">
              <td valign="top">
                <asp:Label ID="FirstNameLabel" runat="server" Text='<%#Eval("FirstName") %>' />
                <asp:Label ID="LastNameLabel" runat="server" Text='<%#Eval("LastName") %>' />
              </td>
              <td>
                <asp:Label ID="EmailLabel" runat="server" Text='<%#Eval("EmailAddress") %>' />
              </td>
              <td>
                <asp:LinkButton ID="EditButton" runat="server" CommandName="Edit" Text="Edit" />
              </td>
            </tr>
          </ItemTemplate>
          <EditItemTemplate>
            <tr style="background-color:#ADD8E6">
              <td valign="top">
                <asp:Label runat="server" ID="FirstNameLabel" 
                  AssociatedControlID="FirstNameTextBox" Text="First Name"/>
                <asp:TextBox ID="FirstNameTextBox" runat="server"
                  Text='<%# Bind("FirstName") %>' MaxLength="50" Width="200px" /><br />
                <asp:Label runat="server" ID="LastNameLabel" 
                  AssociatedControlID="LastNameTextBox" Text="Last Name" />
                <asp:TextBox ID="LastNameTextBox" runat="server" 
                  Text='<%# Bind("LastName") %>' MaxLength="50" Width="200px" /><br />
                <asp:Label runat="server" ID="EmailLabel" 
                  AssociatedControlID="EmailTextBox" Text="Email" />
                <asp:TextBox ID="EmailTextBox" runat="server" 
                  Text='<%# Bind("EmailAddress") %>' MaxLength="50" Width="200px" />
              </td>
              <td colspan="2" valign="top">
                <asp:LinkButton ID="UpdateButton" runat="server" CommandName="Update" Text="Update" />
                <asp:LinkButton ID="CancelButton" runat="server" CommandName="Cancel" Text="Cancel" />
              </td>
            </tr>
          </EditItemTemplate>
      </asp:ListView>

      <!-- 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="ContactsDataSource" runat="server" 
        ConnectionString="<%$ ConnectionStrings:AdventureWorks_DataConnectionString %>"
        SelectCommand="SELECT [ContactID], [FirstName], [LastName], [EmailAddress] 
          FROM Person.Contact"
        UpdateCommand="UPDATE Person.Contact
          Set [FirstName] = @FirstName, [LastName] = @LastName, [EmailAddress] = @EmailAddress 
          WHERE [ContactID] = @ContactID">
      </asp:SqlDataSource>
      
    </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 Page_Load()
        Message.Text = String.Empty
    End Sub

    '<Snippet2>
    Sub ContactsListView_ItemUpdated(sender As Object, e As ListViewUpdatedEventArgs)
        If e.Exception IsNot Nothing Then
            If e.AffectedRows = 0 Then
                e.KeepInEditMode = True
                Message.Text = "An exception occurred updating the contact. " & _
                                    "Please verify your values and try again."
            Else
                Message.Text = "An exception occurred updating the contact. " & _
                                    "Please verify the values in the recently updated item."
            End If

            e.ExceptionHandled = True
        End If
    End Sub
   '</Snippet2>
    
</script>

<html xmlns="http://www.w3.org/1999/xhtml" >
  <head id="Head1" runat="server">
    <title>ListView.ItemUpdated Example</title>
</head>
<body>
    <form id="form1" runat="server">
        
      <h3>ListView.ItemUpdated Example</h3>
            
      <asp:Label ID="Message"
        ForeColor="Red"          
        runat="server"/>
      <br/>

      <asp:ListView ID="ContactsListView" 
        DataSourceID="ContactsDataSource" 
        DataKeyNames="ContactID"
        OnItemUpdated="ContactsListView_ItemUpdated"  
        runat="server">
        <LayoutTemplate>
          <table cellpadding="2" border="1" runat="server" id="tblContacts" width="640px">
            <tr runat="server" id="itemPlaceholder" />
          </table>
          <asp:DataPager runat="server" ID="PeopleDataPager" PageSize="12">
             <Fields>
               <asp:NextPreviousPagerField 
                ShowFirstPageButton="True" ShowLastPageButton="True"
                    FirstPageText="|&lt;&lt; " LastPageText=" &gt;&gt;|"
                    NextPageText=" &gt; " PreviousPageText=" &lt; " />
             </Fields>
           </asp:DataPager>
         </LayoutTemplate>
         <ItemTemplate>
            <tr>
              <td valign="top">
                <asp:Label ID="FirstNameLabel" runat="server" Text='<%#Eval("FirstName") %>' />
                <asp:Label ID="LastNameLabel" runat="server" Text='<%#Eval("LastName") %>' />
              </td>
              <td>
                <asp:Label ID="EmailLabel" runat="server" Text='<%#Eval("EmailAddress") %>' />
              </td>
              <td>
                <asp:LinkButton ID="EditButton" runat="server" CommandName="Edit" Text="Edit" />
              </td>
            </tr>
          </ItemTemplate>
          <EditItemTemplate>
            <tr style="background-color:#ADD8E6">
              <td valign="top">
                <asp:Label runat="server" ID="FirstNameLabel" 
                  AssociatedControlID="FirstNameTextBox" Text="First Name"/>
                <asp:TextBox ID="FirstNameTextBox" runat="server"
                  Text='<%# Bind("FirstName") %>' MaxLength="50" Width="200px" /><br />
                <asp:Label runat="server" ID="LastNameLabel" 
                  AssociatedControlID="LastNameTextBox" Text="Last Name" />
                <asp:TextBox ID="LastNameTextBox" runat="server" 
                  Text='<%# Bind("LastName") %>' MaxLength="50" Width="200px" /><br />
                <asp:Label runat="server" ID="EmailLabel" 
                  AssociatedControlID="EmailTextBox" Text="Email" />
                <asp:TextBox ID="EmailTextBox" runat="server" 
                  Text='<%# Bind("EmailAddress") %>' MaxLength="50" Width="200px" />
              </td>
              <td colspan="2" valign="top">
                <asp:LinkButton ID="UpdateButton" runat="server" CommandName="Update" Text="Update" />
                <asp:LinkButton ID="CancelButton" runat="server" CommandName="Cancel" Text="Cancel" />
              </td>
            </tr>
          </EditItemTemplate>
      </asp:ListView>

      <!-- 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="ContactsDataSource" runat="server" 
        ConnectionString="<%$ ConnectionStrings:AdventureWorks_DataConnectionString %>"
        SelectCommand="SELECT [ContactID], [FirstName], [LastName], [EmailAddress] 
          FROM Person.Contact"
        UpdateCommand="UPDATE Person.Contact
          Set [FirstName] = @FirstName, [LastName] = @LastName, [EmailAddress] = @EmailAddress 
          WHERE [ContactID] = @ContactID">
      </asp:SqlDataSource>
      
    </form>
  </body>
</html>

Remarques

L’événement ItemUpdated est déclenché lorsque vous cliquez sur le bouton Mettre à jour d’un élément ou que la UpdateItem méthode est appelée, une fois que le ListView contrôle a mis à jour l’élément. (Un bouton Mettre à jour est un contrôle bouton dont CommandName la propriété est définie sur « Mettre à jour ».) Cela vous permet d’effectuer une routine personnalisée chaque fois que cet événement se produit, comme la vérification des résultats de l’opération de mise à jour.

Un ListViewUpdatedEventArgs objet est passé au gestionnaire d’événements, ce qui vous permet de déterminer le nombre de lignes affectées. Il vous permet également de déterminer quelles exceptions ont pu se produire. Vous pouvez indiquer si l’exception a été gérée dans le gestionnaire d’événements en définissant la ExceptionHandled propriété de l’objet ListViewUpdatedEventArgs .

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

S’applique à

Voir aussi