Duration (Pacific Standard Time):
To (Pacific Standard Time):
Impact:
User Action:
.NET Framework Class Library

ListView.SelectedItemTemplate Property

Gets or sets the custom content for the selected item in a ListView control.

Namespace:  System.Web.UI.WebControls
Assembly:  System.Web.Extensions (in System.Web.Extensions.dll)
<BrowsableAttribute(False)> _
<PersistenceModeAttribute(PersistenceMode.InnerProperty)> _
<TemplateContainerAttribute(GetType(ListViewDataItem), BindingDirection.TwoWay)> _
Public Overridable Property SelectedItemTemplate As ITemplate
<asp:ListView>
	<SelectedItemTemplate>ITemplate</SelectedItemTemplate>
</asp:ListView>

Property Value

Type: System.Web.UI.ITemplate
An object that contains the custom content for the selected item in a ListView control. The default is null, which indicates that this property is not set.

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

To specify the custom template declaratively, add a SelectedItemTemplate element inside the 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 Expressions Overview.

You can also add buttons to the SelectedItemTemplate. To create buttons that perform the built-in delete or edit operations, add a button control to the template and set its 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 SelectedItemTemplate property to define the contents of the selected item in the ListView control.

<%@ Page language="VB" %>

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

<script runat="server">
    Protected Sub CategoriesListView_OnItemDeleting(sender As Object, e As ListViewDeleteEventArgs)
        If SubCategoriesGridView.Rows.Count > 0 Then
            MessageLabel.Text = "You cannot delete a category that has sub-categories."
            e.Cancel = True
        End If
    End Sub

    Protected Sub Page_Load()
        MessageLabel.Text = String.Empty
    End Sub
</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

Supported in: 4.6, 4, 3.5

Windows 8.1, Windows Server 2012 R2, Windows 8, Windows Server 2012, Windows 7, Windows Vista SP2, Windows Server 2008 (Server Core Role not supported), Windows Server 2008 R2 (Server Core Role supported with SP1 or later; Itanium not supported)

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