ListViewEditEventArgs Class
Collapse the table of content
Expand the table of content

ListViewEditEventArgs Class


Provides data for the ItemEditing event.

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


public class ListViewEditEventArgs : CancelEventArgs


Initializes a new instance of the ListViewEditEventArgs class.


Gets or sets a value indicating whether the event should be canceled.(Inherited from CancelEventArgs.)


Gets the index of the item that is being edited.


Determines whether the specified object is equal to the current object.(Inherited from Object.)


Allows an object to try to free resources and perform other cleanup operations before it is reclaimed by garbage collection.(Inherited from Object.)


Serves as the default hash function. (Inherited from Object.)


Gets the Type of the current instance.(Inherited from Object.)


Creates a shallow copy of the current Object.(Inherited from Object.)


Returns a string that represents the current object.(Inherited from Object.)

The ListView control raises the ItemEditing event when an Edit button is clicked, but before the ListView item enters edit mode. (An Edit button is a button whose CommandName property set to "Edit".) This enables you to provide an event-handling method that performs a custom routine whenever this event occurs, such as canceling the edit operation.

A ListViewEditEventArgs object is passed to the event-handling method. This object enables you to determine the index of the item that is being edited, and to indicate that the edit operation should be canceled. To cancel the edit operation, set the Cancel property of the ListViewEditEventArgs object to true.

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

The following example shows how to use the ListViewEditEventArgs object to cancel the edit operation when the user tries to update a discontinued item.

System_CAPS_security Security Note

This example has a text box that accepts 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;

  void ProductsListView_ItemEditing(Object sender, ListViewEditEventArgs e)
    ListViewItem item = ProductsListView.Items[e.NewEditIndex];
    Label dateLabel = (Label)item.FindControl("DiscontinuedDateLabel");

    if (String.IsNullOrEmpty(dateLabel.Text))

    //Verify if the item is discontinued.
    DateTime discontinuedDate = DateTime.Parse(dateLabel.Text);
    if (discontinuedDate < DateTime.Now)
      Message.Text = "You cannot edit a discontinued item.";
      e.Cancel = true;
      ProductsListView.SelectedIndex = -1;

  void DiscontinuedDateCalendar_OnSelectionChanged(Object sender, EventArgs e)
    TextBox dateTextBox = 
    Calendar calendarObject = (Calendar)sender;
    dateTextBox.Text = calendarObject.SelectedDate.ToString("d");

  DateTime GetDateTime(object dateValue)
    if (dateValue == DBNull.Value)
      return DateTime.Now;
      return (DateTime)dateValue;

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

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

      <h3>ListView Edit Item Example</h3>

      <asp:Label ID="Message"

      <asp:ListView ID="ProductsListView" 
        runat="server" >
          <table cellpadding="2" runat="server" id="tblProducts" width="640px">
            <tr runat="server" id="itemPlaceholder" />
          <asp:DataPager runat="server" ID="ProductsDataPager" 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" />
            <td valign="top">
              <asp:Label ID="NameLabel" runat="server" Text='<%#Eval("Name") %>' />
            <td valign="top">
              <asp:Label ID="ProductNumberLabel" runat="server" Text='<%#Eval("ProductNumber") %>' />
              <asp:Label ID="DiscontinuedDateLabel" runat="server" 
                Text='<%#Eval("DiscontinuedDate", "{0:d}") %>' />
          <tr style="background-color:#ADD8E6">
            <td valign="top">
              <asp:LinkButton ID="UpdateButton" runat="server" 
                CommandName="Update" Text="Update" /><br />
              <asp:LinkButton ID="CancelButton" runat="server" 
                CommandName="Cancel" Text="Cancel" />
            <td valign="top" colspan="2">
              <asp:Label runat="server" ID="NameLabel" 
              <asp:TextBox ID="NameTextBox" runat="server" 
                Text='<%#Bind("Name") %>' MaxLength="50" /><br />
              <asp:Label runat="server" ID="ProductNumberLabel" 
                Text="Product Number" />
              <asp:TextBox ID="ProductNumberTextBox" runat="server" 
                Text='<%#Bind("ProductNumber") %>' MaxLength="25" /><br />
              <asp:Label runat="server" ID="DiscontinuedDateLabel" 
                Text="Discontinued Date"/>
              <asp:TextBox ID="DiscontinuedDateTextBox" runat="server" 
                Text='<%# Bind("DiscontinuedDate", "{0:d}") %>'
                MaxLength="10" /><br />
              <asp:Calendar ID="DiscontinuedDateCalendar" runat="server" 
                SelectedDate='<%# GetDateTime(Eval("DiscontinuedDate")) %>'
                OnSelectionChanged="DiscontinuedDateCalendar_OnSelectionChanged" />

      <!-- 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="ProductsDataSource" runat="server" 
        ConnectionString="<%$ ConnectionStrings:AdventureWorks_DataConnectionString %>"
        SelectCommand="SELECT [ProductID], [Name], [ProductNumber], [DiscontinuedDate] 
          FROM Production.Product"
        UpdateCommand="UPDATE Production.Product
          SET Name = @Name, ProductNumber = @ProductNumber, DiscontinuedDate = @DiscontinuedDate
          WHERE ProductID = @ProductID">


.NET Framework
Available since 3.5

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

Return to top
© 2015 Microsoft