Learning Resources
 

Editing records with EditItemTemplate


Use the EditItemTemplate property to define a custom user interface (UI) for the item in edit mode. The EditItemTemplate template usually contains the input controls for the user to update the values of an existing record. It also usually contains buttons to update the record and to cancel the update operation.

To specify the custom template declaratively, add an EditItemTemplate element inside the ListView control. Then add the contents and controls for template to the EditItemTemplate element. You can associate a field with an input control by using a two-way binding expression. This enables the ListView control to automatically display the original field value in the associated input control for the item in edit mode. When a record is updated, the ListView control can automatically extract the updated field value from the associated input control.

To create buttons that perform the built-in cancel and update operations, add a button control to the template. Set its CommandName property to one of the values listed in the following table.

 

Button type

CommandName value

Cancel

"Cancel"

Update

"Update"

The following example shows how to define a custom template for the item in edit mode.

C#

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

    "https://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">


 
    ListView Templates Example
 
 
   



     

ListView Templates Example



              DataSourceID="ContactsDataSource"
        DataKeyNames="ContactID"
        runat="server">
       
         
           
             
             
             
           
           
         
ActionFirst NameLast Name

         
           
                              FirstPageText="|<< " LastPageText=" >>|"
                NextPageText=" > " PreviousPageText=" < " />
           

         

       

       
         
           
             
           
           
             
           
           
             
           
         
       

       
         
           
               
             
           
           
                              MaxLength="50" />

           
           
                              MaxLength="50" />

           
         
       

     


     
     
     
     
              ConnectionString="<%$ ConnectionStrings:AdventureWorks_DataConnectionString %>"
        SelectCommand="SELECT [ContactID], [FirstName], [LastName] FROM Person.Contact"
        UpdateCommand="UPDATE Person.Contact
                       SET FirstName = @FirstName, LastName = @LastName
                       WHERE ContactID = @ContactID">