This documentation is archived and is not being maintained.

TemplateField.InsertItemTemplate Property

Gets or sets the template for displaying an item in insert mode in a TemplateField object.

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

[TemplateContainerAttribute(typeof(IDataItemContainer), BindingDirection.TwoWay)]
public virtual ITemplate InsertItemTemplate { get; set; }

Property Value

Type: System.Web.UI.ITemplate
A System.Web.UI.ITemplate-implemented object that contains the template for displaying an item in insert mode in a TemplateField. The default is null, which indicates that this property is not set.

Use the InsertItemTemplate property to specify the custom content displayed for an item in insert mode in a TemplateField object. Define the content by creating a template that specifies how the item in insert mode is rendered.

To specify a template, first place opening and closing <InsertItemTemplate> tags between the opening and closing tags of the <TemplateField> element. Next, add the custom content between the opening and closing <InsertItemTemplate> tags. The content can be as simple as plain text or more complex (embedding other controls in the template, for example).

To programmatically access a control defined in a template, first determine which TableCell object in the data-bound control contains the control. Next, use the Controls collection of the TableCell object to access the control. You can also use the FindControl method of the TableCell object to find the control, if the control has an ID property specified.


Not all data-bound controls support this template This template is supported only by data-bound controls that allow you to insert a record, such as the DetailsView control.

The following code example demonstrates how to use the InsertItemTemplate property to create a custom template for an item in insert mode in a TemplateField field row in a DetailsView control. The template displays a DropDownList control that allows the user to select a value from a predefined list.

<%@ Page language="C#" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
<script runat="server">

  void StoresGridView_SelectedIndexChanged(Object sender, EventArgs e)
    // Set the DataItemIndex property of the DetailsView control to display
    // the record selected from the GridView control.
   // StoresDetailView.DataItemIndex = StoresGridView.SelectedIndex;

  void StoresDetailView_ItemInserting(Object sender, DetailsViewInsertEventArgs e)
    // Get the state value from the DropDownList control in the 
    // DetailsView control.
    String state = GetState(); 

    // Add the state to the dictionary of values to 
    // insert into the database.
    e.Values["state"] = state;

  void StoresDetailView_ItemInserted (Object sender, DetailsViewInsertedEventArgs e)
    // Refresh the GridView control after a new record is inserted.
    StoresGridView.DataBind ();

  String GetState()
    String state;

    // Get the DropDownList control that contains the state value
    // in the DetailsView control.
    DropDownList list = (DropDownList)StoresDetailView.Rows[4].FindControl("StateList");

    if (list != null)
      // Get the selected value of the DropDownList control.
      state = list.SelectedItem.Text;
      // Set the state to an empty string ("").
      state = "";

    return state;


<html xmlns="" >
  <head runat="server">
    <title>TemplateField InsertItemTemplate Example</title>
    <form id="form1" runat="server">

      <h3>TemplateField InsertItemTemplate Example</h3>

        <table cellspacing="10">



              <asp:gridview id="StoresGridView" 

                <headerstyle backcolor="Blue"


                  <asp:boundfield datafield="stor_name"
                    headertext="Store Name"/>

                  <asp:boundfield datafield="stor_address"

                  <asp:boundfield datafield="city"

                  <asp:boundfield datafield="state"

                  <asp:boundfield datafield="zip"
                    headertext="ZIP Code"/>




            <td valign="top">

              <asp:detailsview id="StoresDetailView"

                <headerStyle backcolor="Navy"

                  <asp:boundfield datafield="stor_id"
                    headertext="Store ID"/>

                  <asp:boundfield datafield="stor_name"
                    headertext="Store Name"/>

                  <asp:boundfield datafield="stor_address"

                  <asp:boundfield datafield="city"

                  <asp:templatefield headertext="State">
                      <asp:dropdownlist id="StateList"

                  <asp:boundfield datafield="zip"
                    headertext="ZIP Code"/>





        <!-- This example uses Microsoft SQL Server and connects -->
        <!-- to the Pubs sample database.                        -->
        <asp:sqldatasource id="StoresSqlDataSource"  
          selectcommand="SELECT [stor_id], [stor_name], [stor_address], [city], [state], [zip] FROM [stores]" 
          connectionstring="server=localhost;database=pubs;integrated security=SSPI"

        <asp:sqldatasource id="StoresDetailsSqlDataSource"  
          selectcommand="SELECT [stor_id], [stor_name], [stor_address], [city], [state], [zip] FROM [stores]"
          insertcommand="INSERT INTO stores([stor_id], [stor_name], [stor_address], [city], [state], [zip]) VALUES (@stor_id, @stor_name, @stor_address, @city, @state, @zip)" 
          connectionstring="server=localhost;database=pubs;integrated security=SSPI"

        <!-- For this example, the states are retrieved from the  -->
        <!-- state field. For your application, you should use a  -->
        <!-- more complete source for the state values.           -->
        <asp:sqldatasource id="StateSqlDataSource"  
          selectcommand="SELECT Distinct [state] FROM [stores]"
          connectionstring="server=localhost;database=pubs;integrated security=SSPI"


.NET Framework

Supported in: 4, 3.5, 3.0, 2.0

Windows 7, Windows Vista SP1 or later, Windows XP SP3, Windows XP SP2 x64 Edition, 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.