ListViewInsertEventArgs Classe

Definizione

Fornisce i dati per l'evento ItemInserting.

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

Esempio

Nell'esempio seguente viene illustrato come utilizzare l'oggetto ListViewInsertEventArgs per annullare un'operazione di inserimento se l'elemento dati contiene un valore vuoto.

Importante

Questo esempio contiene una casella di testo che accetta l'input dell'utente, che rappresenta una potenziale minaccia per la 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_ItemInserting(Object sender, ListViewInsertEventArgs e)
  {
    // Iterate through the values to verify if they are not empty.
    foreach (DictionaryEntry de in e.Values)
    {
      if (de.Value == null)
      {
        Message.Text = "Cannot insert an empty value.";
        e.Cancel = true;
      }
    }
  }
  //</Snippet2>

</script>

<html xmlns="http://www.w3.org/1999/xhtml" >
  <head id="Head1" runat="server">
    <title>ListView.ItemInserting Example</title>
  </head>
  <body>
    <form id="form1" runat="server">
        
      <h3>ListView.ItemInserting Example</h3>
            
      <asp:Label ID="Message"
        ForeColor="Red"          
        runat="server"/>
      <br/>
     
      <asp:ListView ID="ContactsListView" 
        DataSourceID="ContactsDataSource" 
        DataKeyNames="ContactID"
        OnItemInserting="ContactsListView_ItemInserting"  
        InsertItemPosition="LastItem"
        runat="server">
        <LayoutTemplate>
          <table cellpadding="2" border="1" runat="server" id="tblProducts" 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") %>' />
              &nbsp;
            </td>
            <td>
              <asp:Label ID="EmailLabel" runat="server" Text='<%#Eval("EmailAddress") %>' />
              &nbsp;
            </td>
          </tr>
        </ItemTemplate>
        <InsertItemTemplate>
          <tr style="background-color:#B0C4DE">
            <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" /><br />
              <asp:Label runat="server" ID="LastNameLabel" 
                AssociatedControlID="LastNameTextBox" Text="Last Name" />
              <asp:TextBox ID="LastNameTextBox" runat="server" 
                Text='<%#Bind("LastName") %>' MaxLength="50" /><br />
              <asp:Label runat="server" ID="EmailLabel" 
                AssociatedControlID="EmailTextBox" Text="Email" />
              <asp:TextBox ID="EmailTextBox" runat="server" 
                Text='<%#Bind("EmailAddress") %>' MaxLength="50" /><br />
            </td>
            <td>
              <asp:LinkButton ID="InsertButton" runat="server" CommandName="Insert" Text="Insert" />
            </td>
          </tr>
        </InsertItemTemplate>
      </asp:ListView>
            
      <asp:SqlDataSource ID="ContactsDataSource" runat="server" 
        ConnectionString="<%$ ConnectionStrings:AdventureWorks_DataConnectionString %>"
        SelectCommand="SELECT [ContactID], [FirstName], [LastName], [EmailAddress] 
          FROM Person.Contact"
        InsertCommand="INSERT INTO Person.Contact
                       ([FirstName], [LastName], [EmailAddress], [PasswordHash], [PasswordSalt]) 
                       Values(@FirstName, @LastName, @EmailAddress, '', '');
                       SELECT @ContactID = SCOPE_IDENTITY()">
        <InsertParameters>
          <asp:Parameter Name="FirstName" />
          <asp:Parameter Name="LastName" />
          <asp:Parameter Name="EmailAddress" />
          <asp:Parameter Name="ContactID" Type="Int32" Direction="Output" />
        </InsertParameters>
      </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_ItemInserting(ByVal sender As Object, _
                                     ByVal e As ListViewInsertEventArgs)
  
    ' Iterate through the values to verify if they are not empty.
    For Each de As DictionaryEntry In e.Values
      If de.Value Is Nothing Then
        Message.Text = "Cannot insert an empty value."
        e.Cancel = True
      End If
    Next
  End Sub
  '</Snippet2>

</script>

<html xmlns="http://www.w3.org/1999/xhtml" >
  <head id="Head1" runat="server">
    <title>ListView.ItemInserting Example</title>
  </head>
  <body>
    <form id="form1" runat="server">
        
      <h3>ListView.ItemInserting Example</h3>
            
      <asp:Label ID="Message"
        ForeColor="Red"          
        runat="server"/>
      <br/>
     
      <asp:ListView ID="ContactsListView" 
        DataSourceID="ContactsDataSource" 
        DataKeyNames="ContactID"
        OnItemInserting="ContactsListView_ItemInserting"  
        InsertItemPosition="LastItem"
        runat="server">
        <LayoutTemplate>
          <table cellpadding="2" border="1" runat="server" id="tblProducts" 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") %>' />
              &nbsp;
            </td>
            <td>
              <asp:Label ID="EmailLabel" runat="server" Text='<%#Eval("EmailAddress") %>' />
              &nbsp;
            </td>
          </tr>
        </ItemTemplate>
        <InsertItemTemplate>
          <tr style="background-color:#B0C4DE">
            <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" /><br />
              <asp:Label runat="server" ID="LastNameLabel" 
                AssociatedControlID="LastNameTextBox" Text="Last Name" />
              <asp:TextBox ID="LastNameTextBox" runat="server" 
                Text='<%#Bind("LastName") %>' MaxLength="50" /><br />
              <asp:Label runat="server" ID="EmailLabel" 
                AssociatedControlID="EmailTextBox" Text="Email" />
              <asp:TextBox ID="EmailTextBox" runat="server" 
                Text='<%#Bind("EmailAddress") %>' MaxLength="50" /><br />
            </td>
            <td>
              <asp:LinkButton ID="InsertButton" runat="server" CommandName="Insert" Text="Insert" />
            </td>
          </tr>
        </InsertItemTemplate>
      </asp:ListView>
            
      <asp:SqlDataSource ID="ContactsDataSource" runat="server" 
        ConnectionString="<%$ ConnectionStrings:AdventureWorks_DataConnectionString %>"
        SelectCommand="SELECT [ContactID], [FirstName], [LastName], [EmailAddress] 
          FROM Person.Contact"
        InsertCommand="INSERT INTO Person.Contact
                       ([FirstName], [LastName], [EmailAddress], [PasswordHash], [PasswordSalt]) 
                       Values(@FirstName, @LastName, @EmailAddress, '', '');
                       SELECT @ContactID = SCOPE_IDENTITY()">
        <InsertParameters>
          <asp:Parameter Name="FirstName" />
          <asp:Parameter Name="LastName" />
          <asp:Parameter Name="EmailAddress" />
          <asp:Parameter Name="ContactID" Type="Int32" Direction="Output" />
        </InsertParameters>
      </asp:SqlDataSource>
      
    </form>
  </body>
</html>

Commenti

Il ListView controllo genera l'evento ItemInserting quando si fa clic su un pulsante Inserisci nel controllo, ma prima che il ListView controllo inserisca il record. Un pulsante Inserisci è un pulsante la cui CommandName proprietà è impostata su "Inserisci".) In questo modo è possibile fornire un metodo di gestione degli eventi che esegue una routine personalizzata ogni volta che si verifica questo evento. Ad esempio, è possibile convalidare o codificaRE HTML i valori di un record prima di inserirli nell'origine dati.

Un ListViewInsertEventArgs oggetto viene passato al metodo di gestione degli eventi. Questo oggetto consente di determinare il valore di un argomento di comando facoltativo inviato al ListView controllo. È possibile accedere all'oggetto ListViewItem inserito utilizzando la Item proprietà . È anche possibile leggere o modificare i valori dei campi per il nuovo record usando la Values proprietà . Per annullare l'operazione di inserimento, impostare la Cancel proprietà su true.

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

Costruttori

ListViewInsertEventArgs(ListViewItem)

Inizializza una nuova istanza della classe ListViewInsertEventArgs.

Proprietà

Cancel

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

(Ereditato da CancelEventArgs)
Item

Ottiene un oggetto ListViewItem che rappresenta l'elemento dei dati da inserire.

Values

Ottiene i valori per il record da inserire.

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