Export (0) Print
Expand All

DataList.SelectedItem Property

Gets the selected item in the DataList control.

Namespace:  System.Web.UI.WebControls
Assembly:  System.Web (in System.Web.dll)

'Declaration
<BrowsableAttribute(False)> _
Public Overridable ReadOnly Property SelectedItem As DataListItem

Property Value

Type: System.Web.UI.WebControls.DataListItem
A DataListItem that represents the item selected in the DataList control.

Use the SelectedItem property to get a DataListItem object that represents the selected item in the DataList control. This object can then be used to access the properties of the selected item.

The following code example demonstrates how to use the SelectedItem property to display the contents of the selected item in the DataList control.

NoteNote

The following code sample uses the single-file code model and may not work correctly if copied directly into a code-behind file. This code sample must be copied into an empty text file that has an .aspx extension. For more information on the Web Forms code model, see ASP.NET Web Forms Page Code Model.

<%@ Page Language="VB" AutoEventWireup="True" %>
<%@ Import Namespace="System.Data" %>

<!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>
    <title>DataList SelectedItem Example</title>
<script language="VB" runat="server">

    Dim Cart As DataTable
    Dim CartView As DataView

    Sub Page_Load(sender As Object, e As EventArgs)
        If Session("DL3_ShoppingCart") Is Nothing Then
            Cart = New DataTable()
            Cart.Columns.Add(New DataColumn("Qty", GetType(String)))
            Cart.Columns.Add(New DataColumn("Item", GetType(String)))
            Cart.Columns.Add(New DataColumn("Price", GetType(String)))
            Session("DL3_ShoppingCart") = Cart

            ' First Load - Create some data.
            Dim i As Integer
            For i = 1 To 9
                Dim dr As DataRow = Cart.NewRow()
                dr(0) =(i Mod 2 + 1).ToString()
                dr(1) = "Item " & i.ToString()
                dr(2) =(1.23 *(i + 1)).ToString()
                Cart.Rows.Add(dr)
            Next i
        Else
            Cart = CType(Session("DL3_ShoppingCart"), DataTable)
        End If 
        CartView = New DataView(Cart)
        CartView.Sort = "Item"
        If Not IsPostBack Then
            BindList()
        End If
    End Sub 'Page_Load

    Sub BindList()
        DataList1.DataSource = CartView
        DataList1.DataBind()
    End Sub 'BindList

    Sub DataList_ItemCommand(sender As Object, e As DataListCommandEventArgs)
        DataList1.SelectedIndex = e.Item.ItemIndex
        BindList()
        Label4.Text = "You selected: " & CType(DataList1.SelectedItem.FindControl("Label1"), Label).Text
    End Sub 'DataList_ItemCommand

    Sub Button_Click(sender As Object, e As EventArgs)
        If Not (DataList1.SelectedItem Is Nothing) Then
            DataList1.SelectedItem.ForeColor = System.Drawing.Color.Red
        End If
    End Sub 'Button_Click

</script>

</head>
<body>

   <form id="form1" runat="server">

      <h3>DataList SelectedItem Example</h3>
      <p></p>

      <asp:DataList id="DataList1" runat="server"
           GridLines="Both"
           CellPadding="3"
           CellSpacing="0"           
           OnItemCommand="DataList_ItemCommand">

         <HeaderStyle BackColor="#aaaadd">
         </HeaderStyle>

         <AlternatingItemStyle BackColor="Gainsboro">
         </AlternatingItemStyle>

         <SelectedItemStyle BackColor="lightgreen">
         </SelectedItemStyle>

         <HeaderTemplate>

            Items

         </HeaderTemplate>

         <ItemTemplate>

            <asp:LinkButton id="button1" 
                 Text="Select" 
                 CommandName="select"
                 runat="server"/>
            <%# (CType(Container.DataItem, DataRowView))("Item")%>

         </ItemTemplate>

         <SelectedItemTemplate>

            Item:
            <asp:Label id="Label1" 
                 Text='<%# (CType(Container.DataItem, DataRowView))("Item")%>' 
                 runat="server"/>

            <br />

            Quantity:
            <asp:Label id="Label2" 
                 Text='<%# (CType(Container.DataItem, DataRowView))("Qty")%>' 
                 runat="server"/>

            <br />

            Price:
            <asp:Label id="Label3" 
                 Text='<%# DataBinder.Eval(Container.DataItem, "Price")%>' 
                 runat="server"/>

         </SelectedItemTemplate>

      </asp:DataList>

      <br /><br />

      <asp:Label id="Label4"
           runat="server"/>

      <br /><br />

      <asp:Button id="Button2"
           Text="Change Text to Red" 
           OnClick="Button_Click"
           runat="server"/>

   </form>

</body>
</html>

.NET Framework

Supported in: 4.5.2, 4.5.1, 4.5, 4, 3.5, 3.0, 2.0, 1.1, 1.0

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.

Show:
© 2014 Microsoft