Este artigo foi traduzido por máquina. Para visualizar o arquivo em inglês, marque a caixa de seleção Inglês. Você também pode exibir o texto Em inglês em uma janela pop-up, movendo o ponteiro do mouse sobre o texto.
Tradução
Inglês

Propriedade ListView.ItemTemplate

 

Obtém ou define o conteúdo personalizado para o item de dados em um controle ListView.

Namespace:   System.Web.UI.WebControls
Assembly:  System.Web.Extensions (em System.Web.Extensions.dll)

[BrowsableAttribute(false)]
[PersistenceModeAttribute(PersistenceMode.InnerProperty)]
[TemplateContainerAttribute(typeof(ListViewDataItem), BindingDirection.TwoWay)]
public virtual ITemplate ItemTemplate { get; set; }

Valor da Propriedade

Type: System.Web.UI.ITemplate

Um objeto que contém o conteúdo personalizado para o item de dados em um controle ListView. O padrão é null, que indica que essa propriedade não está definida.

Use the P:System.Web.UI.WebControls.ListView.ItemTemplate property to define a custom user interface (UI) to display the data items. The P:System.Web.UI.WebControls.ListView.ItemTemplate template is required by the T:System.Web.UI.WebControls.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 P:System.Web.UI.WebControls.ListView.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 T:System.Web.UI.WebControls.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. For more information, see Data-Binding Expression Syntax.

To create buttons that perform the built-in select, delete, and edit operations, add a button control to the template. Set its P:System.Web.UI.WebControls.Button.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 T:System.Web.UI.WebControls.ListView control.

System_CAPS_security Segurança Observação

This example has a text box that accepts 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="C#" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
    "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml" >
  <head id="Head1" runat="server">
    <title>ListView Templates Example</title>
  </head>
  <body>
    <form id="form1" runat="server">

      <h3>ListView Templates Example</h3>

      <asp:ListView ID="ContactsListView" 
        DataSourceID="ContactsDataSource" 
        DataKeyNames="ContactID"
        runat="server">
        <LayoutTemplate>
          <table cellpadding="2" width="640px" border="1" runat="server" id="tblProducts">
            <tr runat="server">
              <th runat="server">Action</th>
              <th runat="server">First Name</th>
              <th runat="server">Last Name</th>
            </tr>
            <tr runat="server" id="itemPlaceholder" />
          </table>
          <asp:DataPager runat="server" ID="ContactsDataPager" PageSize="12">
            <Fields>
              <asp:NextPreviousPagerField ShowFirstPageButton="true" ShowLastPageButton="true"
                FirstPageText="|&lt;&lt; " LastPageText=" &gt;&gt;|"
                NextPageText=" &gt; " PreviousPageText=" &lt; " />
            </Fields>
          </asp:DataPager>
        </LayoutTemplate>
        <ItemTemplate>
          <tr runat="server">
            <td>
              <asp:LinkButton ID="EditButton" runat="Server" Text="Edit" CommandName="Edit" />
            </td>
            <td>
              <asp:Label ID="FirstNameLabel" runat="Server" Text='<%#Eval("FirstName") %>' />
            </td>
            <td valign="top">
              <asp:Label ID="LastNameLabel" runat="Server" Text='<%#Eval("LastName") %>' />
            </td>
          </tr>
        </ItemTemplate>
        <EditItemTemplate>
          <tr style="background-color: #ADD8E6">
            <td>
              <asp:LinkButton ID="UpdateButton" runat="server" CommandName="Update" Text="Update" />&nbsp;
              <asp:LinkButton ID="CancelButton" runat="server" CommandName="Cancel" Text="Cancel" />
            </td>
            <td>
              <asp:TextBox ID="FirstNameTextBox" runat="server" Text='<%#Bind("FirstName") %>' 
                MaxLength="50" /><br />
            </td>
            <td>
              <asp:TextBox ID="LastNameTextBox" runat="server" Text='<%#Bind("LastName") %>' 
                MaxLength="50" /><br />
            </td>
          </tr>
        </EditItemTemplate>
      </asp:ListView>

      <!-- 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="ContactsDataSource" runat="server" 
        ConnectionString="<%$ ConnectionStrings:AdventureWorks_DataConnectionString %>"
        SelectCommand="SELECT [ContactID], [FirstName], [LastName] FROM Person.Contact"
        UpdateCommand="UPDATE Person.Contact
                       SET FirstName = @FirstName, LastName = @LastName
                       WHERE ContactID = @ContactID">
      </asp:SqlDataSource>

    </form>
  </body>
</html>

.NET Framework
Disponível desde 3.5
Retornar ao início
Mostrar: