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 Overridable ReadOnly Property InsertItem As ListViewItem

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.

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

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

<script runat="server">

  Protected Sub CountriesListView_ItemInserting(ByVal sender As Object, _
                                                ByVal e As ListViewInsertEventArgs)

    ' Get the controls that are contained in the insert item.
    Dim countryCodeTextBox As TextBox = _
      CType(CountriesListView.InsertItem.FindControl("CountryCodeTextBox"), TextBox)
    Dim nameTextBox As TextBox = _
      CType(CountriesListView.InsertItem.FindControl("NameTextBox"), TextBox)

    'Check if the controls are empty.
    If countryCodeTextBox.Text.Trim().Length = 0 _
    OrElse nameTextBox.Text.Trim().Length = 0 Then
      MessageLabel.Text = "The system could not insert the item. All fields are required."
      e.Cancel = True
    End If

  End Sub

  Protected Sub Page_Load(ByVal sender As Object, ByVal e As EventArgs) 
    'Clear the message label.
    MessageLabel.Text = ""
  End Sub


<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
Available since 3.5
Return to top