This documentation is archived and is not being maintained.

ListView.ItemCanceling Event

Occurs when a cancel operation is requested, but before the ListView control cancels the insert or edit operation.

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

public event EventHandler<ListViewCancelEventArgs> ItemCanceling
<asp:ListView OnItemCanceling="EventHandler" />

The ListView control raises the ItemCanceling event when a Cancel button in the InsertItemTemplate or EditItemTemplate template is clicked, but before the ListView control cancels the insert or edit operation. (A Cancel button is a button control whose CommandName property is set to "Cancel".) This enables you to perform a custom routine whenever this event occurs, such as stopping the cancel operation if it would put the item in an unwanted state.

A ListViewCancelEventArgs object is passed to the event handler, which enables you to determine the index of the item. It also lets you indicate that the cancel operation should be stopped. To stop the operation, set the Cancel property of the ListViewCancelEventArgs object to true.

For more information about how to handle events, see Consuming Events.

The following example shows how to create an ItemCanceling event handler.

Security noteSecurity Note:

This example contains text boxes that accept user input, which is a potential security threat. By default, ASP.NET Web pages validate that user input does not include script or HTML elements. For more information, see Script Exploits Overview.

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

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" 

<script runat="server">

  void Page_Load()
    Message.Text = String.Empty;

  protected void ContactsListView_ItemCanceling(object sender, ListViewCancelEventArgs e)
    //Check the operation that raised the event 
    if (e.CancelMode == ListViewCancelMode.CancelingEdit)
      // The update operation was canceled. Display the  
      // primary key of the item.
      Message.Text = "Update for the ContactID " + 
        ContactsListView.DataKeys[e.ItemIndex].Value.ToString()  + " canceled.";
      Message.Text = "Insert operation canceled."; 

  protected void ContactsListView_PagePropertiesChanging(object sender, 
    PagePropertiesChangingEventArgs e)
    // Clears the edit index selection when paging.
    ContactsListView.EditIndex = -1;

<html xmlns="" >
  <head id="Head1" runat="server">
    <title>ListView ItemCanceling Example</title>
    <form id="form1" runat="server">

      <h3>ListView ItemCanceling Example</h3>

      <asp:Label ID="Message"

      <asp:ListView ID="ContactsListView" 
        OnPagePropertiesChanging="ContactsListView_PagePropertiesChanging" >
          <table cellpadding="2" width="680px" border="1" runat="server" id="tblContacts">
            <tr runat="server">
              <th runat="server">&nbsp;</th>
              <th runat="server">ID</th>
              <th runat="server">First Name</th>
              <th runat="server">Last Name</th>
              <th runat="server">E-mail Address</th>
            <tr runat="server" id="itemPlaceholder" />
          <asp:DataPager runat="server" ID="ContactsDataPager" PageSize="12">
                ShowFirstPageButton="true" ShowLastPageButton="true"
                FirstPageText="|&lt;&lt; " LastPageText=" &gt;&gt;|"
                NextPageText=" &gt; " PreviousPageText=" &lt; " />
          <tr runat="server">
            <td valign="top">
              <asp:LinkButton ID="EditButton" runat="server" Text="Edit" CommandName="Edit" /><br />
            <td valign="top">
              <asp:Label ID="Label1" runat="server" Text='<%#Eval("ContactID") %>' />
            <td valign="top">
              <asp:Label ID="FirstNameLabel" runat="server" Text='<%#Eval("FirstName") %>' />
            <td valign="top">
              <asp:Label ID="LastNameLabel" runat="server" Text='<%#Eval("LastName") %>' />
            <td valign="top">
              <asp:Label ID="EmailAddressLabel" runat="server" Text='<%#Eval("EmailAddress") %>' />
          <tr style="background-color: #ADD8E6">
            <td valign="top">
              <asp:LinkButton ID="UpdateButton" runat="server" CommandName="Update" Text="Update" /><br />
              <asp:LinkButton ID="CancelEditButton" runat="server" CommandName="Cancel" Text="Cancel" />
              <asp:Label ID="Label1" runat="server" Text='<%#Eval("ContactID") %>' />
              <asp:TextBox ID="FirstNameTextBox" runat="server" 
                Text='<%#Bind("FirstName") %>' MaxLength="50" /><br />
              <asp:TextBox ID="LastNameTextBox" runat="server" 
                Text='<%#Bind("LastName") %>' MaxLength="50" /><br />
              <asp:TextBox ID="EmailAddressTextBox" runat="server" 
                Text='<%#Bind("EmailAddress") %>' MaxLength="50" /><br />
          <tr style="background-color:#90EE90">
            <td colspan="2">
              <asp:LinkButton ID="InsertButton" runat="server" CommandName="Insert" Text="Insert" /><br />
              <asp:LinkButton ID="CancelInsertButton" runat="server" CommandName="Cancel" Text="Cancel" />
              <asp:TextBox ID="FirstNameITextBox" runat="server" 
                Text='<%#Bind("FirstName") %>' MaxLength="50" />
              <asp:TextBox ID="LastNameITextBox" runat="server" 
                Text='<%#Bind("LastName") %>' MaxLength="50" />
              <asp:TextBox ID="EmailAddressITextBox" runat="server" 
                Text='<%#Bind("EmailAddress") %>' MaxLength="50" />

      <!-- 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"
        InsertCommand="INSERT INTO Person.Contact
                         ([FirstName], [LastName], [EmailAddress], [PasswordHash], [PasswordSalt]) 
                         Values(@FirstName, @LastName, @EmailAddress, '', '')"
        UpdateCommand="UPDATE Person.Contact
                         SET FirstName = @FirstName, LastName = @LastName,
                         EmailAddress = @EmailAddress
                         WHERE ContactID = @ContactID">


Windows 7, Windows Vista, Windows XP SP2, Windows Server 2008 R2, Windows Server 2008, Windows Server 2003

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

.NET Framework

Supported in: 3.5