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.SelectedItemTemplate

 

Obtém ou define o conteúdo personalizado para o item selecionado em um controle de 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 SelectedItemTemplate { get; set; }

Valor da Propriedade

Type: System.Web.UI.ITemplate

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

Use the P:System.Web.UI.WebControls.ListView.SelectedItemTemplate property to define a custom user interface (UI) for the selected item in a T:System.Web.UI.WebControls.ListView control. The P:System.Web.UI.WebControls.ListView.SelectedItemTemplate is usually used to differentiate the selected item from the other displayed items. P:System.Web.UI.WebControls.ListView.SelectedItemTemplate is rendered instead of the P:System.Web.UI.WebControls.ListView.ItemTemplate template for the selected item.

To specify the custom template declaratively, add a SelectedItemTemplate element inside the T:System.Web.UI.WebControls.ListView control. You can then add the contents of the template to the SelectedItemTemplate 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.

You can also add buttons to the P:System.Web.UI.WebControls.ListView.SelectedItemTemplate. To create buttons that perform the built-in delete or edit operations, add a button control to the template and 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"

The following example shows how to use the P:System.Web.UI.WebControls.ListView.SelectedItemTemplate property to define the contents of the selected item in the T:System.Web.UI.WebControls.ListView control.

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

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

<script runat="server">
  protected void CategoriesListView_OnItemDeleting(object sender, ListViewDeleteEventArgs e)
  {
    if (SubCategoriesGridView.Rows.Count > 0)
    {
      MessageLabel.Text = "You cannot delete a category that has sub-categories.";
      e.Cancel = true;
    }
  }

  protected void Page_Load()
  {
    MessageLabel.Text = String.Empty;
  }
</script>

<html xmlns="http://www.w3.org/1999/xhtml" >
  <head id="Head1" runat="server">
    <title>Subcategories List</title>
  </head>
  <body>
    <form id="form1" runat="server">
      <b>Categories</b>
      <br />

      <asp:Label ForeColor="Red" runat="server" ID="MessageLabel" /><br />

      <asp:ListView runat="server" 
        ID="CategoriesListView"
        OnItemDeleting="CategoriesListView_OnItemDeleting"
        DataSourceID="CategoriesDataSource" 
        DataKeyNames="ProductCategoryID">
        <LayoutTemplate>
          <table runat="server" id="tblCategories" 
                 cellspacing="0" cellpadding="1" width="440px" border="1">
            <tr id="itemPlaceholder" runat="server"></tr>
          </table>
        </LayoutTemplate>
        <ItemTemplate>
          <tr runat="server">
            <td>
              <asp:Label runat="server" ID="NameLabel" Text='<%#Eval("Name") %>' />
            </td>
            <td style="width:40px">
              <asp:LinkButton runat="server" ID="SelectCategoryButton" 
                Text="Select" CommandName="Select" />
            </td>
          </tr>
        </ItemTemplate>
        <SelectedItemTemplate>
          <tr runat="server" style="background-color:#90EE90">
            <td>
              <asp:Label runat="server" ID="NameLabel" Text='<%#Eval("Name") %>' />
            </td>
            <td style="width:40px">
              <asp:LinkButton runat="server" ID="SelectCategoryButton" 
                Text="Delete" CommandName="Delete" />
            </td>
          </tr>
        </SelectedItemTemplate>
      </asp:ListView>

      <br />

      <b>Subcategories</b>
      <asp:GridView runat="server" ID="SubCategoriesGridView" Width="300px"
           DataSourceID="SubCategoriesDataSource" DataKeyNames="ProductSubcategoryID" 
           AutoGenerateColumns="True" />

      <!-- 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="CategoriesDataSource" runat="server" 
        ConnectionString="<%$ ConnectionStrings:AdventureWorks_DataConnectionString %>"
        SelectCommand="SELECT [ProductCategoryID], [Name]
                       FROM Production.ProductCategory">
      </asp:SqlDataSource>
      <asp:SqlDataSource ID="SubCategoriesDataSource" runat="server" 
        ConnectionString="<%$ ConnectionStrings:AdventureWorks_DataConnectionString %>"
        SelectCommand="SELECT [ProductSubcategoryID], [Name]
                       FROM Production.ProductSubcategory
                       WHERE ProductCategoryID = @ProductCategoryID
                       ORDER BY [Name]">
          <SelectParameters>
            <asp:ControlParameter Name="ProductCategoryID" DefaultValue="0"
                 ControlID="CategoriesListView" PropertyName="SelectedValue"  />
          </SelectParameters>
      </asp:SqlDataSource>
    </form>
  </body>
</html>

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