ListViewDeleteEventArgs Clase

Definición

Proporciona datos para el 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
Herencia
ListViewDeleteEventArgs

Ejemplos

En el ejemplo siguiente se muestra cómo usar el ListViewDeleteEventArgs objeto para cancelar la operación de eliminación si el usuario intenta quitar el último elemento de un ListView control.

<%@ 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>

Comentarios

El ListView control genera el ItemDeleting evento cuando se hace clic en un botón Eliminar o se llama al DeleteItem método , pero antes de que el ListView control elimine el elemento. (Un botón Eliminar es uno cuya CommandName propiedad está establecida en "Eliminar"). Esto le permite proporcionar un método de control de eventos que realiza una rutina personalizada cada vez que se produce este evento, como cancelar la operación de eliminación.

Un ListViewDeleteEventArgs objeto se pasa al método de control de eventos, lo que permite determinar el índice del elemento que se va a eliminar. También puede cancelar la operación de eliminación. Para ello, establezca la Cancel propiedad del ListViewDeleteEventArgs objeto en true. Si es necesario, también puede trabajar con las Keys colecciones y Values antes de pasar los valores al origen de datos.

Para obtener una lista de valores de propiedad iniciales para una instancia de ListViewDeleteEventArgs, vea el ListViewDeleteEventArgs constructor .

Constructores

ListViewDeleteEventArgs(Int32)

Inicializa una nueva instancia de la clase ListViewDeleteEventArgs.

Propiedades

Cancel

Obtiene o establece un valor que indica si se debe cancelar el evento.

(Heredado de CancelEventArgs)
ItemIndex

Obtiene el índice del elemento que se va a eliminar.

Keys

Obtiene un diccionario de pares de nombre y valor de campo que representan la clave o las claves principales del elemento que se va a eliminar.

Values

Obtiene un diccionario de los pares de nombre y valor de campo no de clave del elemento que se va a eliminar.

Métodos

Equals(Object)

Determina si el objeto especificado es igual que el objeto actual.

(Heredado de Object)
GetHashCode()

Sirve como la función hash predeterminada.

(Heredado de Object)
GetType()

Obtiene el Type de la instancia actual.

(Heredado de Object)
MemberwiseClone()

Crea una copia superficial del Object actual.

(Heredado de Object)
ToString()

Devuelve una cadena que representa el objeto actual.

(Heredado de Object)

Se aplica a

Consulte también