This documentation is archived and is not being maintained.

ListView.InsertItem Property

Gets the insert item of a ListView control.

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

public virtual ListViewItem InsertItem { get; }

Property Value

Type: System.Web.UI.WebControls.ListViewItem
An object that represents the insert item of a ListView control.

Use the InsertItem property to access the insert item of a ListView control. An insert item is a ListViewItem object whose ItemType is equal to ListViewItemType.InsertItem. The content of an insert item is defined by the InsertItemTemplate property.

The following example shows how to get the insert item by using the InsertItem property to access controls that are contained in the InsertItemTemplate template.

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

  protected void CountriesListView_ItemInserting(object sender, ListViewInsertEventArgs e)
    // Get the controls that are contained in the insert item.
    TextBox countryCodeTextBox = 
    TextBox nameTextBox = 

    //Check if the controls are empty.
    if ((countryCodeTextBox.Text.Trim().Length == 0) ||
      (nameTextBox.Text.Trim().Length == 0))
      MessageLabel.Text = 
        "The system could not insert the item. All fields are required.";
      e.Cancel = true;


  protected void Page_Load(object sender, EventArgs e)
    //Clear the message label.
    MessageLabel.Text = "";

<html xmlns="" >
  <head id="Head1" runat="server">
    <title>ListView InsertItem Example</title>
    <style type="text/css">
      body  { font: 10pt Trebuchet MS, Arial, Tahoma; }
      th { background: #8FBC8F; }
      .item td { border: 1px solid #8FBC8F; }
      .insertItem td  { background: #D3D3D3; }
    <form id="form1" runat="server">

      <h3>ListView InsertItem Example</h3>

      <asp:ListView ID="CountriesListView" 
          <table cellpadding="4" width="500" runat="server" id="tblCountries">
            <tr runat="server">
              <th runat="server">Code</th>
              <th runat="server">Name</th>
            <tr runat="server" id="itemPlaceholder" />
          <asp:DataPager ID="CountriesPager" runat="server" PageSize="20">
              <asp:NumericPagerField ButtonCount="10" />
          <tr class="item" runat="server">
              <asp:Label ID="CountryCodeLabel" runat="server" 
                Text='<%# Eval("CountryRegionCode")%>' />
              <asp:Label ID="NameLabel" runat="server" 
                Text='<%# Eval("Name")%>' />
          <tr class="insertItem">
            <td align="right">Code:</td>
            <td align="left">
              <asp:TextBox ID="CountryCodeTextBox" runat="server" 
                Text='<%# Bind("CountryRegionCode")%>' 
                MaxLength="3" />
          <tr class="insertItem">
            <td align="right">Name:</td>
            <td align="left">
              <asp:TextBox ID="NameTextBox" runat="server" 
                Text='<%# Bind("Name")%>' 
                MaxLength="50" />
          <tr class="insertItem" runat="server">
            <td colspan="2" align="center">
              <asp:Button ID="InsertButton" runat="server" 
                CommandName="Insert" Text="Insert" />
              <asp:Button ID="CancelButton" runat="server" 
                CommandName="Cancel" Text="Clear" />
      <br /><br />

      <asp:Label ID="MessageLabel"
        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="CountryDataSource" runat="server" 
        ConnectionString="<%$ ConnectionStrings:AdventureWorks_DataConnectionString %>"
        SelectCommand="SELECT [CountryRegionCode], [Name]
          FROM [Person].[CountryRegion]"
        InsertCommand="INSERT INTO Person.CountryRegion(CountryRegionCode, Name) 
          VALUES (@CountryRegionCode, @Name)">


.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.