Export (0) Print
Expand All

DetailsViewDeleteEventArgs Class

Note: This class is new in the .NET Framework version 2.0.

Provides data for the ItemDeleting event.

Namespace: System.Web.UI.WebControls
Assembly: System.Web (in system.web.dll)

public class DetailsViewDeleteEventArgs : CancelEventArgs
public class DetailsViewDeleteEventArgs extends CancelEventArgs
public class DetailsViewDeleteEventArgs extends CancelEventArgs

The DetailsView control raises the ItemDeleting event when a Delete button (a button with its CommandName property set to "Delete") within the control is clicked, but before the DetailsView control deletes the record. This allows you to provide an event handler that performs a custom routine, such as verifying a record before deleting it, whenever this event occurs.

A DetailsViewDeletedEventArgs object is passed to the event handler, which allows you to determine the index of the record being deleted and to indicate that the delete operation should be canceled. To determine the index of a record, use the RowIndex property. To cancel the delete operation, set the Cancel property to true. You can also access the key fields and non-key fields by using the Keys and Values properties, respectively. These values are useful if you want to verify the record before deleting it.

NoteNote

It is possible to modify the key field values in the Keys property. If you change these values, the record that corresponds to the new key field values will be deleted.

For more information about handling events, see Consuming Events.

For a list of initial property values for an instance of DetailsViewDeleteEventArgs class, see the DetailsViewDeleteEventArgs constructor.

The following code example demonstrates how to use the DetailsViewDeleteEventArgs object passed to the event handler for the ItemDeleting event to cancel a delete operation.


<%@ Page language="C#" %>

<script runat="server">

  void CustomerDetailsView_ItemDeleting(Object sender, 
    DetailsViewDeleteEventArgs e)
  {

    // Get customer ID and name from the Keys and Values
    // properties.
    String keyValue = e.Keys["CustomerID"].ToString();
    String customerName = e.Values["CompanyName"].ToString();

    // Cancel the delete operation if the user attempts to 
    // delete protected record. In this example, records
    // with a customer ID that starts with with "A" cannot
    // be deleted.
    if (keyValue.StartsWith("A"))
    {
      e.Cancel = true;
      MessageLabel.Text = "You cannot delete " +
        customerName + ". This customer is protected.";
    }
    else
    {
      MessageLabel.Text = "Row " + e.RowIndex.ToString() + 
        " deleted.";
    }

  }

</script>

<html>
  <body>
    <form runat="server">
        
      <h3>DetailsViewDeleteEventArgs Example</h3>
                
        <asp:detailsview id="CustomerDetailsView"
          datasourceid="DetailsViewSource"
          datakeynames="CustomerID"
          autogeneratedeletebutton="true"  
          autogeneraterows="true"
          allowpaging="true"
          onitemdeleting="CustomerDetailsView_ItemDeleting" 
          runat="server">
            
          <fieldheaderstyle backcolor="Navy"
            forecolor="White"/>
                    
        </asp:detailsview>
        
        <asp:label id="MessageLabel"
          forecolor="Red"
          runat="server"/>
            
        <!-- This example uses Microsoft SQL Server and connects  -->
        <!-- to the Northwind sample database. Use an ASP.NET     -->
        <!-- expression to retrieve the connection string value   -->
        <!-- from the web.config file.                            -->
        <asp:sqldatasource id="DetailsViewSource"
          selectcommand="Select [CustomerID], [CompanyName], [Address], 
            [City], [PostalCode], [Country] From [Customers]"
          deletecommand="Delete [Customers] 
            Where [CustomerID]=@CustomerID"
          connectionstring=
            "<%$ ConnectionStrings:NorthWindConnectionString%>" 
          runat="server"/>
            
      </form>
  </body>
</html>


System.Object
   System.EventArgs
     System.ComponentModel.CancelEventArgs
      System.Web.UI.WebControls.DetailsViewDeleteEventArgs

Any public static (Shared in Visual Basic) members of this type are thread safe. Any instance members are not guaranteed to be thread safe.

Windows 98, Windows 2000 SP4, Windows Server 2003, Windows XP Media Center Edition, Windows XP Professional x64 Edition, Windows XP SP2, Windows XP Starter Edition

The .NET Framework does not support all versions of every platform. For a list of the supported versions, see System Requirements.

.NET Framework

Supported in: 2.0

Community Additions

ADD
Show:
© 2014 Microsoft