ListViewInsertedEventArgs Class


The .NET API Reference documentation has a new home. Visit the .NET API Browser on to see the new experience.

Provides data for the ItemInserted event.

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


public class ListViewInsertedEventArgs : EventArgs

System_CAPS_pubmethodListViewInsertedEventArgs(Int32, Exception)

Initializes a new instance of the ListViewInsertedEventArgs class.


Gets the number of rows affected by the insert operation.


Gets the exception (if any) that was raised during the insert operation.


Gets or sets a that indicates whether an exception that was raised during the insert operation was handled in the event handler.


Gets or sets a value that indicates whether the user's input values are preserved for the controls inside the InsertItemTemplate template.


Gets field name/value pairs for the inserted record.


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 ItemInserted event when an Insert button in the InsertItemTemplate template is clicked, and after the ListView control updates the record at the data source. (An Insert button is a button whose CommandName property is set to "Insert".) The ItemInserted event enables you to perform a custom action, such as retrieving auto-generated values from the database for the inserted item.

A ListViewInsertedEventArgs object is passed to the event-handling method, which enables you to determine the number of items inserted and any exceptions that might have occurred. To determine the number of items affected by the insert operation, use the AffectedRows property. Use the Exception property to determine whether an exception occurred. You can also indicate whether the exception was handled in the event-handling method by setting the ExceptionHandled property. If you have to access the field values of the inserted item that were sent to the data source, use the Values property.

By default, the ListView control clears the InsertItemTemplate template after an insert operation, which enables users to add values for a new item to be inserted. If an exception occurs during the insert operation, you can keep the ListView control in insert mode by setting the KeepInInsertMode property to true. This rebinds the InsertItemTemplate template to the values from the previous attempt to insert an item.

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

The following example shows how to use the ListViewInsertedEventArgs object that is passed to the handler for the ItemInserted 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="C#" %>

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

<script runat="server">

  void ContactsListView_ItemInserted(Object sender, ListViewInsertedEventArgs e)
    if (e.Exception != null)
      if (e.AffectedRows == 0)
        e.KeepInInsertMode = true;
        Message.Text = "An exception occurred inserting the new Contact. " +
          "Please verify your values and try again.";
        Message.Text = "An exception occurred inserting the new Contact. " +
          "Please verify the values in the newly inserted item.";

      e.ExceptionHandled = true;

  protected void Page_Load(object sender, EventArgs e)
    Message.Text = "";

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

      <h3>ListViewItemInserted Example</h3>

      <asp:Label ID="Message"

      <asp:ListView ID="ContactsListView" 
          <table cellpadding="2" border="1" runat="server" id="tblContacts" width="640px">
            <tr runat="server" id="itemPlaceholder" />
          <asp:DataPager runat="server" ID="PeopleDataPager" PageSize="12">
                ShowFirstPageButton="true" ShowLastPageButton="true"
                FirstPageText="|&lt;&lt; " LastPageText=" &gt;&gt;|"
                NextPageText=" &gt; " PreviousPageText=" &lt; " />
          <tr runat="server">
            <td valign="top">
              <asp:Label ID="FirstNameLabel" runat="server" Text='<%#Eval("FirstName") %>' />
              <asp:Label ID="LastNameLabel" runat="server" Text='<%#Eval("LastName") %>' />
              <asp:Label ID="EmailLabel" runat="server" Text='<%#Eval("EmailAddress") %>' />
          <tr style="background-color:#D3D3D3">
            <td valign="top">
              <asp:Label runat="server" ID="FirstNameLabel" 
                AssociatedControlID="FirstNameTextBox" Text="First Name"/>
              <asp:TextBox ID="FirstNameTextBox" runat="server" 
                Text='<%#Bind("FirstName") %>' /><br />
              <asp:Label runat="server" ID="LastNameLabel" 
                AssociatedControlID="LastNameTextBox" Text="Last Name" />
              <asp:TextBox ID="LastNameTextBox" runat="server" 
                Text='<%#Bind("LastName") %>' /><br />
              <asp:Label runat="server" ID="EmailLabel" 
                AssociatedControlID="EmailTextBox" Text="E-mail" />
              <asp:TextBox ID="EmailTextBox" runat="server" 
                Text='<%#Bind("EmailAddress") %>' />
              <asp:LinkButton ID="InsertButton" runat="server" 
                CommandName="Insert" Text="Insert" />

      <!-- 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, '', '');
          SELECT @ContactID = SCOPE_IDENTITY()">
          <asp:Parameter Name="FirstName" />
          <asp:Parameter Name="LastName" />
          <asp:Parameter Name="EmailAddress" />
          <asp:Parameter Name="ContactID" Type="Int32" Direction="Output" />


.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