ListViewUpdateEventArgs Classe

Definizione

Fornisce i dati per l'evento ItemUpdating.

public ref class ListViewUpdateEventArgs : System::ComponentModel::CancelEventArgs
public class ListViewUpdateEventArgs : System.ComponentModel.CancelEventArgs
type ListViewUpdateEventArgs = class
    inherit CancelEventArgs
Public Class ListViewUpdateEventArgs
Inherits CancelEventArgs
Ereditarietà
ListViewUpdateEventArgs

Esempio

Nell'esempio seguente viene illustrato come usare la NewValues proprietà per assicurarsi che l'utente abbia fornito tutti i valori prima che l'origine dati venga aggiornata.

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_ItemUpdating(Object sender, ListViewUpdateEventArgs e)
  {
    // Cancel the update operation if any of the fields is empty
    // or null.
    foreach (DictionaryEntry de in e.NewValues)
    {
      // Check if the value is null or empty.
      if (de.Value == null || de.Value.ToString().Trim().Length == 0)
      {
        Message.Text = "Cannot set a field to an empty value.";
        e.Cancel = true;
      }
    }
    
    // Convert the email address to lowercase.
    String emailValue = e.NewValues["EmailAddress"].ToString();
    e.NewValues["EmailAddress"] = emailValue.ToLower();

  }
  //</Snippet2>
  
</script>

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

      <asp:ListView ID="ContactsListView" 
        DataSourceID="ContactsDataSource" 
        DataKeyNames="ContactID"
        OnItemUpdating="ContactsListView_ItemUpdating"  
        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" Width="200px"
                Text='<%#Bind("FirstName") %>' MaxLength="50" /><br />
              <asp:Label runat="server" ID="LastNameLabel" 
                AssociatedControlID="LastNameTextBox" Text="Last Name" />
              <asp:TextBox ID="LastNameTextBox" runat="server" Width="200px"
                Text='<%#Bind("LastName") %>' MaxLength="50" /><br />
              <asp:Label runat="server" ID="EmailLabel"
                AssociatedControlID="EmailTextBox" Text="Email" />
              <asp:TextBox ID="EmailTextBox" runat="server" Width="200px"
                Text='<%#Bind("EmailAddress") %>' MaxLength="50" />
            </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_ItemUpdating(ByVal sender As Object, ByVal e As ListViewUpdateEventArgs)
    
    ' Cancel the update operation if any of the fields is empty
    ' or null.
    For Each de As DictionaryEntry In e.NewValues
      ' Check if the value is null or empty
      If de.Value Is Nothing OrElse de.Value.ToString().Trim().Length = 0 Then
        Message.Text = "Cannot set a field to an empty value."
        e.Cancel = True
      End If
    Next
    
    ' Convert the email address to lowercase.
    Dim emailValue As String = e.NewValues("EmailAddress").ToString()    
    e.NewValues("EmailAddress") = emailValue.ToLower()
    
  End Sub
  '</Snippet2>
  
</script>

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

      <asp:ListView ID="ContactsListView" 
        DataSourceID="ContactsDataSource" 
        DataKeyNames="ContactID"
        OnItemUpdating="ContactsListView_ItemUpdating"  
        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" Width="200px"
                Text='<%#Bind("FirstName") %>' MaxLength="50" /><br />
              <asp:Label runat="server" ID="LastNameLabel" 
                AssociatedControlID="LastNameTextBox" Text="Last Name" />
              <asp:TextBox ID="LastNameTextBox" runat="server" Width="200px"
                Text='<%#Bind("LastName") %>' MaxLength="50" /><br />
              <asp:Label runat="server" ID="EmailLabel"
                AssociatedControlID="EmailTextBox" Text="Email" />
              <asp:TextBox ID="EmailTextBox" runat="server" Width="200px"
                Text='<%#Bind("EmailAddress") %>' MaxLength="50" />
            </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 ItemUpdatingUpdateItem metodo o quando viene fatto clic sul pulsante Update di un elemento, ma prima che il controllo aggiorni 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 annullando l'operazione di aggiornamento.

Un ListViewUpdateEventArgs oggetto viene passato al metodo di gestione degli eventi. Questo oggetto consente di determinare l'indice dell'elemento corrente e di specificare che l'operazione di aggiornamento deve essere annullata. Per annullare l'operazione di aggiornamento, impostare la Cancel proprietà dell'oggetto ListViewUpdateEventArgs su true. È anche possibile usare le Keysraccolte , OldValuese NewValues prima che i valori vengano passati all'origine dati. Un modo tipico per usare queste raccolte consiste nel convalidare o codificare HTML i valori forniti dall'utente prima che questi valori vengano archiviati nell'origine dati. In questo modo è possibile evitare attacchi di inserimento di script.

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

Costruttori

ListViewUpdateEventArgs(Int32)

Inizializza una nuova istanza della classe ListViewUpdateEventArgs.

Proprietà

Cancel

Ottiene o imposta un valore che indica se l'evento debba essere annullato.

(Ereditato da CancelEventArgs)
ItemIndex

Ottiene il valore dell'elemento dei dati da aggiornare.

Keys

Ottiene un dizionario delle coppie nome/valore dei campi che rappresentano la chiave o le chiavi dell'elemento da aggiornare.

NewValues

Ottiene un dizionario che contiene i valori modificati dell'elemento da aggiornare.

OldValues

Ottiene un dizionario che contiene i valori originali dell'elemento da aggiornare.

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