Certified ASP.NET Programmer Learning Resources Displaying data with ItemTemplate

Learning Resources
 

Displaying data with ItemTemplate


Use the ItemTemplate property to define a custom user interface (UI) to display the data items. The ItemTemplate template is required by the ListView control. It usually contains controls to display the field values of a record. If you want to let users modify the data, you also usually add buttons to the ItemTemplate template that let the user select a record, switch to edit mode, or delete a record.

To specify the custom template declaratively, add an ItemTemplate element inside the ListView control. You can then add the contents of the template to the ItemTemplate element. To display the field values of the data source that is bound to the control, use a data-binding expression.

To create buttons that perform the built-in select, delete, and edit 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

Delete

"Delete"

Edit

"Edit"

Select

"Select"

 

The following example shows how to define a custom template for a data item in the ListView control.

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


   

 

 

 For Support