ListViewDeleteEventArgs Classe

Definizione

Fornisce i dati per l'evento ItemDeleting.

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

Esempio

Nell'esempio seguente viene illustrato come utilizzare l'oggetto ListViewDeleteEventArgs per annullare l'operazione di eliminazione se l'utente tenta di rimuovere l'ultimo elemento da un ListView controllo.

<%@ 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">
  protected void CategoriesListView_OnItemDeleting(object sender, ListViewDeleteEventArgs e)
  {
    if (SubCategoriesGridView.Rows.Count > 0)
    {
      MessageLabel.Text = "You cannot delete a category that has sub-categories.";
      e.Cancel = true;
    }
  }

  protected void Page_Load()
  {
    MessageLabel.Text = String.Empty;
  }
</script>

<html xmlns="http://www.w3.org/1999/xhtml" >
  <head id="Head1" runat="server">
    <title>Subcategories List</title>
  </head>
  <body>
    <form id="form1" runat="server">
      <b>Categories</b>
      <br />
      
      <asp:Label ForeColor="Red" runat="server" ID="MessageLabel" /><br />
      
      <asp:ListView runat="server" 
        ID="CategoriesListView"
        OnItemDeleting="CategoriesListView_OnItemDeleting"
        DataSourceID="CategoriesDataSource" 
        DataKeyNames="ProductCategoryID">
        <LayoutTemplate>
          <table runat="server" id="tblCategories" 
                 cellspacing="0" cellpadding="1" width="440px" border="1">
            <tr id="itemPlaceholder" runat="server"></tr>
          </table>
        </LayoutTemplate>
        <ItemTemplate>
          <tr runat="server">
            <td>
              <asp:Label runat="server" ID="NameLabel" Text='<%#Eval("Name") %>' />
            </td>
            <td style="width:40px">
              <asp:LinkButton runat="server" ID="SelectCategoryButton" 
                Text="Select" CommandName="Select" />
            </td>
          </tr>
        </ItemTemplate>
        <SelectedItemTemplate>
          <tr runat="server" style="background-color:#90EE90">
            <td>
              <asp:Label runat="server" ID="NameLabel" Text='<%#Eval("Name") %>' />
            </td>
            <td style="width:40px">
              <asp:LinkButton runat="server" ID="SelectCategoryButton" 
                Text="Delete" CommandName="Delete" />
            </td>
          </tr>
        </SelectedItemTemplate>
      </asp:ListView>
      
      <br />
      
      <b>Subcategories</b>
      <asp:GridView runat="server" ID="SubCategoriesGridView" Width="300px"
           DataSourceID="SubCategoriesDataSource" DataKeyNames="ProductSubcategoryID" 
           AutoGenerateColumns="True" />
       
      <!-- 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="CategoriesDataSource" runat="server" 
        ConnectionString="<%$ ConnectionStrings:AdventureWorks_DataConnectionString %>"
        SelectCommand="SELECT [ProductCategoryID], [Name]
                       FROM Production.ProductCategory">
      </asp:SqlDataSource>
      <asp:SqlDataSource ID="SubCategoriesDataSource" runat="server" 
        ConnectionString="<%$ ConnectionStrings:AdventureWorks_DataConnectionString %>"
        SelectCommand="SELECT [ProductSubcategoryID], [Name]
                       FROM Production.ProductSubcategory
                       WHERE ProductCategoryID = @ProductCategoryID
                       ORDER BY [Name]">
          <SelectParameters>
            <asp:ControlParameter Name="ProductCategoryID" DefaultValue="0"
                 ControlID="CategoriesListView" PropertyName="SelectedValue"  />
          </SelectParameters>
      </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">
    Protected Sub CategoriesListView_OnItemDeleting(sender As Object, e As ListViewDeleteEventArgs)
        If SubCategoriesGridView.Rows.Count > 0 Then
            MessageLabel.Text = "You cannot delete a category that has sub-categories."
            e.Cancel = True
        End If
    End Sub

    Protected Sub Page_Load()
        MessageLabel.Text = String.Empty
    End Sub
</script>

<html xmlns="http://www.w3.org/1999/xhtml" >
  <head id="Head1" runat="server">
    <title>Subcategories List</title>
  </head>
  <body>
    <form id="form1" runat="server">
      <b>Categories</b>
      <br />
      
      <asp:Label ForeColor="Red" runat="server" ID="MessageLabel" /><br />
      
      <asp:ListView runat="server" 
        ID="CategoriesListView"
        OnItemDeleting="CategoriesListView_OnItemDeleting"
        DataSourceID="CategoriesDataSource" 
        DataKeyNames="ProductCategoryID">
        <LayoutTemplate>
          <table runat="server" id="tblCategories" 
                 cellspacing="0" cellpadding="1" width="440px" border="1">
            <tr id="itemPlaceholder" runat="server"></tr>
          </table>
        </LayoutTemplate>
        <ItemTemplate>
          <tr runat="server">
            <td>
              <asp:Label runat="server" ID="NameLabel" Text='<%#Eval("Name") %>' />
            </td>
            <td style="width:40px">
              <asp:LinkButton runat="server" ID="SelectCategoryButton" 
                Text="Select" CommandName="Select" />
            </td>
          </tr>
        </ItemTemplate>
        <SelectedItemTemplate>
          <tr runat="server" style="background-color:#90EE90">
            <td>
              <asp:Label runat="server" ID="NameLabel" Text='<%#Eval("Name") %>' />
            </td>
            <td style="width:40px">
              <asp:LinkButton runat="server" ID="SelectCategoryButton" 
                Text="Delete" CommandName="Delete" />
            </td>
          </tr>
        </SelectedItemTemplate>
      </asp:ListView>
      
      <br />
      
      <b>Subcategories</b>
      <asp:GridView runat="server" ID="SubCategoriesGridView" Width="300px"
           DataSourceID="SubCategoriesDataSource" DataKeyNames="ProductSubcategoryID" 
           AutoGenerateColumns="True" />
       
      <!-- 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="CategoriesDataSource" runat="server" 
        ConnectionString="<%$ ConnectionStrings:AdventureWorks_DataConnectionString %>"
        SelectCommand="SELECT [ProductCategoryID], [Name]
                       FROM Production.ProductCategory">
      </asp:SqlDataSource>
      <asp:SqlDataSource ID="SubCategoriesDataSource" runat="server" 
        ConnectionString="<%$ ConnectionStrings:AdventureWorks_DataConnectionString %>"
        SelectCommand="SELECT [ProductSubcategoryID], [Name]
                       FROM Production.ProductSubcategory
                       WHERE ProductCategoryID = @ProductCategoryID
                       ORDER BY [Name]">
          <SelectParameters>
            <asp:ControlParameter Name="ProductCategoryID" DefaultValue="0"
                 ControlID="CategoriesListView" PropertyName="SelectedValue"  />
          </SelectParameters>
      </asp:SqlDataSource>
    </form>
  </body>
</html>

Commenti

Il ListView controllo genera l'evento ItemDeleting quando si fa clic su un pulsante Delete o viene chiamato il DeleteItem metodo , ma prima che il ListView controllo elimini l'elemento. Un pulsante Elimina è una la cui CommandName proprietà è impostata su "Elimina".) 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 l'annullamento dell'operazione di eliminazione.

Un ListViewDeleteEventArgs oggetto viene passato al metodo di gestione degli eventi, che consente di determinare l'indice dell'elemento da eliminare. È anche possibile annullare l'operazione di eliminazione. A tale scopo, impostare la Cancel proprietà dell'oggetto ListViewDeleteEventArgs su true. Se necessario, è anche possibile usare le Keys raccolte e Values prima che i valori vengano passati all'origine dati.

Per un elenco dei valori iniziali delle proprietà per un'istanza di ListViewDeleteEventArgs, vedere il ListViewDeleteEventArgs costruttore .

Costruttori

ListViewDeleteEventArgs(Int32)

Inizializza una nuova istanza della classe ListViewDeleteEventArgs.

Proprietà

Cancel

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

(Ereditato da CancelEventArgs)
ItemIndex

Ottiene l'indice dell'elemento da eliminare.

Keys

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

Values

Ottiene un dizionario delle coppie nome/valore dei campi non chiave nell'elemento da eliminare.

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