This documentation is archived and is not being maintained.

ListView.InsertNewItem Method

Inserts the current record in the data source.

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

public virtual void InsertNewItem(
	bool causesValidation


Type: System.Boolean
true to perform page validation when this method is called; otherwise, false.


The ListView control does not have an insert item.


The DataSourceView object associated with the ListView control is null.

Use the InsertNewItem method to programmatically insert an item in the data source. This method is typically used to insert an item from outside the ListView control, such as from a different control on the page.

To use the InsertNewItem method, you must define an InsertItemTemplate template in the ListView control. You must also set the InsertItemPosition property to a value different from InsertItemPosition.None. To specify whether page validation is performed before the insert operation, use the causesValidation parameter.

This method raises the ItemInserted and ItemInserting events.

The following example shows how to use the InsertNewItem method to programmatically insert the contents of the insert item of a ListView control into the data source.

Security noteSecurity 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">

  protected void InsertButton_Click(object sender, EventArgs e)
    // Clears any existing message.
    MessageLabel.Text = "";

    // Use the InsertNewItem method to programmatically insert
    // the current record in the ListView control.

  protected void DepartmentsListView_ItemInserted(object sender, 
    ListViewInsertedEventArgs e)
    // Handles exceptions that might occur
    // during the insert operation.
    if (e.Exception != null)
      if (e.AffectedRows == 0)
        e.KeepInInsertMode = true;
        MessageLabel.Text = "An exception occurred inserting the new department. " +
                            "Please verify your values and try again.";
        MessageLabel.Text = "An exception occurred inserting the new department. " +
                            "Please verify the values in the newly inserted item.";

      e.ExceptionHandled = true;

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

      <h3>ListView InsertNewItem Example</h3>


      <asp:ListView ID="DepartmentsListView" 
        runat="server" >
          <table runat="server" id="tblDepartments" width="640px" border="1">
            <tr runat="server" id="itemPlaceholder" />
          <tr runat="server">
              <asp:Label ID="NameLabel" runat="Server" Text='<%#Eval("Name") %>' />
              <asp:Label ID="GroupNameLabel" runat="Server" Text='<%#Eval("GroupName") %>' />
          <tr style="background-color:#00BFFF">
              <asp:Label runat="server" ID="NameLabel" AssociatedControlID="NameTextBox" 
                Text="Name:" Font-Bold="true"/><br />
              <asp:TextBox ID="NameTextBox" runat="server" Text='<%#Bind("Name") %>' />
              <asp:Label runat="server" ID="GroupNameLabel" AssociatedControlID="GroupNameTextBox" 
                Text="Group Name:" Font-Bold="true" /><br />
              <asp:TextBox ID="GroupNameTextBox" runat="server" 
                Text='<%#Bind("GroupName") %>' MaxLength="50" />
      </asp:ListView><br />

      <asp:Label ID="MessageLabel"
        runat="server" /> <br />

      <asp:Button ID="InsertButton"
        Text="Insert new record"
        runat="server"  />

      <!-- 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="DepartmentsDataSource" runat="server" 
        ConnectionString="<%$ ConnectionStrings:AdventureWorks_DataConnectionString %>"
        SelectCommand="SELECT DepartmentID, Name, GroupName FROM HumanResources.Department"
        InsertCommand="INSERT INTO HumanResources.Department(Name, GroupName) 
	            VALUES (@Name, @GroupName)">


.NET Framework

Supported in: 4, 3.5

Windows 7, Windows Vista SP1 or later, Windows XP SP3, Windows Server 2008 (Server Core not supported), Windows Server 2008 R2 (Server Core supported with SP1 or later), Windows Server 2003 SP2

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