TemplateField.EditItemTemplate Property

Note: This property is new in the .NET Framework version 2.0.

Gets or sets the template for displaying an item in edit 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 EditItemTemplate { get; set; }
/** @property */
public ITemplate get_EditItemTemplate ()

/** @property */
public void set_EditItemTemplate (ITemplate value)

public function get EditItemTemplate () : ITemplate

public function set EditItemTemplate (value : ITemplate)

Property Value

A System.Web.UI.ITemplate-implemented object that contains the template for displaying an item in edit mode in a TemplateField. The default is a null reference (Nothing in Visual Basic), which indicates that this property is not set.

Use the EditItemTemplate property to specify the custom content displayed for an item that is in edit mode in a TemplateField object. Define the content by creating a template that specifies how an item in edit mode is rendered. The EditItemTemplate property usually contains input controls for the user to modify a value in a data source.

To specify a template, first place opening and closing <EditItemTemplate> tags between the opening and closing tags of the <TemplateField> element. Next, add the custom content between the opening and closing <EditItemTemplate> 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.

The following code example demonstrates how to use the EditItemTemplate property to create a custom template for an item in edit mode in a TemplateField field column of a GridView control. The template contains validation server controls to prevent a user from leaving a value field empty.

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

<script runat="server">
  void AuthorsGridView_RowUpdating (Object sender, GridViewUpdateEventArgs e)
    // The GridView control does not automatically extract updated values 
    // from TemplateField column fields. These values must be added manually 
    // to the NewValues dictionary.
    // Get the GridViewRow object that represents the row being edited
    // from the Rows collection of the GridView control.
    int index = AuthorsGridView.EditIndex;
    GridViewRow row = AuthorsGridView.Rows[index];
    // Get the controls that contain the updated values. In this
    // example, the updated values are contained in the TextBox 
    // controls declared in the edit item templates of each TemplateField 
    // column fields in the GridView control.
    TextBox lastName = (TextBox)row.FindControl("LastNameTextBox");
    TextBox firstName = (TextBox)row.FindControl("FirstNameTextBox");
    // Add the updated values to the NewValues dictionary. Use the
    // parameter names declared in the parameterized update query 
    // string for the key names.
    e.NewValues["au_lname"] = lastName.Text;
    e.NewValues["au_fname"] = firstName.Text;    


    <form runat="server">
      <h3>TemplateField EditItemTemplate Example</h3>

      <!-- The GridView control automatically sets the columns     -->
      <!-- specified in the datakeynames attribute as read-only.   -->
      <!-- No input controls are rendered for these columns in     -->
      <!-- edit mode.                                              -->
      <asp:gridview id="AuthorsGridView" 
          <asp:boundfield datafield="au_id"
            headertext="Author ID"
          <asp:templatefield headertext="Last Name"
              <asp:textbox id="LastNameTextBox"
              <asp:requiredfieldvalidator id="LastNameRequiredValidator"
                text="Please enter a last name." 
                runat="server" />                                      
          <asp:templatefield headertext="First Name"
              <asp:textbox id="FirstNameTextBox"
              <asp:requiredfieldvalidator id="FirstNameRequiredValidator"
                text="Please enter a first name."
                runat="server" />                      
          <asp:checkboxfield datafield="contract" 
      <!-- This example uses Microsoft SQL Server and connects -->
      <!-- to the Pubs sample database.                         -->
      <asp:sqldatasource id="AuthorsSqlDataSource"  
        selectcommand="SELECT [au_id], [au_lname], [au_fname], [contract] FROM [authors]"             
        updatecommand="UPDATE authors SET au_lname=@au_lname, au_fname=@au_fname WHERE (authors.au_id = @au_id)" 
        connectionstring="server=localhost;database=pubs;integrated security=SSPI"

Windows 98, Windows 2000 SP4, Windows Server 2003, Windows XP Media Center Edition, Windows XP Professional x64 Edition, Windows XP SP2, Windows XP Starter Edition

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

.NET Framework

Supported in: 2.0