ListView.ItemEditing Event


Occurs when an edit operation is requested, but before the ListView item is put in edit mode.

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

Public Event ItemEditing As EventHandler(Of ListViewEditEventArgs)

The ItemEditing event is raised when an item's Edit button is clicked, but before the ListView item is put in edit mode. (An Edit button is a button control whose CommandName property is set to "Edit".) This enables you to perform a custom routine whenever this event occurs, such as canceling the edit operation.

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

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

The following example shows how to add an event handler for the ItemEditing event.

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="VB" %>

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

<script runat="server">

  Sub Page_Load()
    Message.Text = String.Empty
  End Sub

  Sub ProductsListView_ItemEditing(ByVal sender As Object, ByVal e As ListViewEditEventArgs)
    Dim item As ListViewItem = ProductsListView.Items(e.NewEditIndex)
    Dim dateLabel As Label = CType(item.FindControl("DiscontinuedDateLabel"), Label)

    If String.IsNullOrEmpty(dateLabel.Text) Then _

    'Verify if the item is discontinued.
    Dim discontinuedDate As DateTime = DateTime.Parse(dateLabel.Text)
    If discontinuedDate < DateTime.Now Then
      Message.Text = "You cannot edit a discontinued item."
      e.Cancel = True
      ProductsListView.SelectedIndex = -1
    End If
  End Sub

  Sub DiscontinuedDateCalendar_OnSelectionChanged(ByVal sender As Object, ByVal e As EventArgs)
    Dim dateTextBox As TextBox = _
      CType(ProductsListView.EditItem.FindControl("DiscontinuedDateTextBox"), TextBox)
    Dim calendarObject As Calendar = CType(sender, Calendar)
    dateTextBox.Text = calendarObject.SelectedDate.ToString("d")
  End Sub

  Function GetDateTime(ByVal dateValue As Object) As DateTime
    If dateValue Is DBNull.Value Then
      Return DateTime.Now
      Return CType(dateValue, DateTime)
    End If
  End Function

  Protected Sub ProductsListView_PagePropertiesChanging(ByVal sender As Object, _
                                               ByVal e As PagePropertiesChangingEventArgs)
    ' Clears the edit index selection when paging.
    ProductsListView.EditIndex = -1
  End Sub


<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
Return to top