ListViewUpdatedEventArgs Classe

Definizione

Fornisce i dati per l'evento ItemUpdated.

public ref class ListViewUpdatedEventArgs : EventArgs
public class ListViewUpdatedEventArgs : EventArgs
type ListViewUpdatedEventArgs = class
    inherit EventArgs
Public Class ListViewUpdatedEventArgs
Inherits EventArgs
Ereditarietà
ListViewUpdatedEventArgs

Esempio

Nell'esempio seguente viene illustrato come usare l'oggetto per determinare se si è verificata un'eccezione ListViewUpdatedEventArgs durante un'operazione di aggiornamento.

Importante

L'esempio include una casella di testo che accetta l'input dell'utente e rappresenta quindi una potenziale minaccia alla sicurezza. Per impostazione predefinita, le pagine Web ASP.NET verificano che l'input dell'utente non includa script o elementi HTML. Per altre informazioni, vedere Cenni preliminari sugli attacchi tramite 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>

Commenti

Il ListView controllo genera l'evento quando viene chiamato il ItemUpdatedUpdateItem metodo o quando viene fatto clic su un pulsante Update nel controllo, ma dopo che il controllo aggiorna l'elemento ListView . Un pulsante Update è un pulsante la cui CommandName proprietà è impostata su "Update". Ciò consente di fornire un metodo di gestione eventi che esegue una routine personalizzata ogni volta che si verifica questo evento, ad esempio controllare i risultati di un'operazione di aggiornamento.

Un ListViewUpdatedEventArgs oggetto viene passato al metodo di gestione degli eventi. Questo oggetto consente di determinare il numero di elementi aggiornati e di ottenere eventuali eccezioni che potrebbero verificarsi. Per determinare il numero di elementi interessati dall'operazione di aggiornamento, usare la AffectedRows proprietà . Per determinare se si sono verificate eccezioni, usare la Exception proprietà . È possibile indicare se l'eccezione è stata gestita nel metodo di gestione degli eventi impostando la ExceptionHandled proprietà . È possibile accedere ai valori dei campi originali usando la OldValues proprietà . È possibile accedere ai valori di campo aggiornati usando la NewValues proprietà .

Per impostazione predefinita, l'elemento ListView restituisce la modalità di sola lettura dopo un'operazione di aggiornamento. Se si gestisce un'eccezione che si è verificata durante l'operazione di aggiornamento, è possibile mantenere l'elemento ListView in modalità di modifica impostando la KeepInEditMode proprietà su true.

Per un elenco dei valori iniziali delle proprietà di un'istanza della classe ListViewUpdatedEventArgs, vedere il costruttore ListViewUpdatedEventArgs.

Costruttori

ListViewUpdatedEventArgs(Int32, Exception)

Inizializza una nuova istanza della classe ListViewUpdatedEventArgs.

Proprietà

AffectedRows

Ottiene il numero di righe interessate dall'operazione di aggiornamento.

Exception

Ottiene l'eventuale eccezione generata durante l'operazione di aggiornamento.

ExceptionHandled

Ottiene o imposta un valore che indica se un'eccezione generata durante l'operazione di aggiornamento è stata gestita durante l'evento.

KeepInEditMode

Ottiene o imposta un valore che indica se il controllo ListView deve rimanere in modalità di modifica dopo un'operazione di aggiornamento.

NewValues

Ottiene un dizionario che contiene i nuovi valori per l'elemento aggiornato.

OldValues

Ottiene un dizionario che contiene i valori originali per l'elemento aggiornato.

Metodi

Equals(Object)

Determina se l'oggetto specificato è uguale all'oggetto corrente.

(Ereditato da Object)
GetHashCode()

Funge da funzione hash predefinita.

(Ereditato da Object)
GetType()

Ottiene l'oggetto Type dell'istanza corrente.

(Ereditato da Object)
MemberwiseClone()

Crea una copia superficiale dell'oggetto Object corrente.

(Ereditato da Object)
ToString()

Restituisce una stringa che rappresenta l'oggetto corrente.

(Ereditato da Object)

Si applica a

Vedi anche