DataList.SelectedItemStyle Property
Gets the style properties for the selected item in the DataList control.
[Visual Basic] Public Overridable ReadOnly Property SelectedItemStyle As _ TableItemStyle [C#] public virtual TableItemStyle SelectedItemStyle {get;} [C++] public: __property virtual TableItemStyle* get_SelectedItemStyle(); [JScript] public function get SelectedItemStyle() : TableItemStyle;
Property Value
A TableItemStyle object that contains the style properties for the selected item in the DataList control. The default value is an empty TableItemStyle object.
Remarks
Use this property to provide a custom style for the selected item in the DataList control. Common style attributes that can be adjusted include forecolor, backcolor, font, and content alignment within the cell. Providing a different style enhances the appearance of the DataList control.
Item style properties in the DataList control are inherited from one item style property to another through a hierarchy. Item style properties set lower in the hierarchy are inherited by item style properties higher in the hierarchy. For example, if you specify a red font for the ItemStyle property, all other item style properties in the DataList control will also have a red font. This allows you to provide a common appearance for the control by setting a single item style property. You can override the inherited style settings for an item style property that is higher in the hierarchy by setting its style properties. For example, you can specify a blue font for the AlternatingItemStyle property, overriding the red font specified in the ItemStyle property. The following table lists the hierarchy order from highest to lowest.
| Precedence | Style Property |
|---|---|
| 1 | EditItemStyle |
| 2 | SelectedItemStyle |
| 3 | AlternatingItemStyle |
| 4 | ItemStyle |
| 5 | ControlStyle |
To specify a custom style for the selcted item, place the <SelectedItemStyle> tags between the opening and closing tags of the DataList control. You can then list the style attributes within the opening <SelectedItemStyle> tag.
Example
[Visual Basic, C#] The following example demonstrates how to use the SelectedItemStyle property to specify a custom background color for the selected item in the DataList control.
[Visual Basic, C#] Note 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 Web Forms Code Model.
[Visual Basic] <%@ Page Language="VB" AutoEventWireup="True" %> <%@ Import Namespace="System.Data" %> <html> <head> <script runat="server"> Function CreateDataSource() As ICollection ' Create sample data for the DataList control. Dim dt As DataTable = New DataTable() Dim dr As DataRow ' Define the columns of the table. dt.Columns.Add(New DataColumn("Item", GetType(Int32))) dt.Columns.Add(New DataColumn("Qty", GetType(Int32))) dt.Columns.Add(New DataColumn("Price", GetType(Double))) ' Populate the table with sample values. Dim i As Integer For i = 0 To 8 dr = dt.NewRow() dr(0) = i dr(1) = i * 2 dr(2) = 1.23 * (i + 1) dt.Rows.Add(dr) Next i Dim dv As DataView = New DataView(dt) Return dv End Function Sub Page_Load(sender As Object, e As EventArgs) ' Load sample data only once, when the page is first loaded. If Not IsPostBack Then ItemsList.DataSource = CreateDataSource() ItemsList.DataBind() End If End Sub Sub Item_Command(sender As Object, e As DataListCommandEventArgs) ' Set the SelectedIndex property to select an item in the DataList. ItemsList.SelectedIndex = e.Item.ItemIndex ' Rebind the data source to the DataList to refresh the control. ItemsList.DataSource = CreateDataSource() ItemsList.DataBind() End Sub </script> </head> <body> <form runat="server"> <h3>DataList Select Example</h3> Click <b>Select</b> to select an item. <br><br> <asp:DataList id="ItemsList" GridLines="Both" CellPadding="3" CellSpacing="0" OnItemCommand="Item_Command" runat="server"> <HeaderStyle BackColor="#aaaadd"> </HeaderStyle> <AlternatingItemStyle BackColor="Gainsboro"> </AlternatingItemStyle> <SelectedItemStyle BackColor="Yellow"> </SelectedItemStyle> <HeaderTemplate> Items </HeaderTemplate> <ItemTemplate> <asp:LinkButton id="SelectButton" Text="Select" CommandName="Select" runat="server"/> Item <%# DataBinder.Eval(Container.DataItem, "Item") %> </ItemTemplate> <SelectedItemTemplate> Item: <asp:Label id="ItemLabel" Text='<%# DataBinder.Eval(Container.DataItem, "Item") %>' runat="server"/> <br> Quantity: <asp:Label id="QtyLabel" Text='<%# DataBinder.Eval(Container.DataItem, "Qty") %>' runat="server"/> <br> Price: <asp:Label id="PriceLabel" Text='<%# DataBinder.Eval(Container.DataItem, "Price", "{0:c}") %>' runat="server"/> </SelectedItemTemplate> </asp:DataList> </form> </body> </html> [C#] <%@ Page Language="C#" AutoEventWireup="True" %> <%@ Import Namespace="System.Data" %> <html> <head> <script runat="server"> ICollection CreateDataSource() { // Create sample data for the DataList control. DataTable dt = new DataTable(); DataRow dr; // Define the columns of the table. dt.Columns.Add(new DataColumn("Item", typeof(Int32))); dt.Columns.Add(new DataColumn("Qty", typeof(Int32))); dt.Columns.Add(new DataColumn("Price", typeof(double))); // Populate the table with sample values. for (int i = 0; i < 9; i++) { dr = dt.NewRow(); dr[0] = i; dr[1] = i * 2; dr[2] = 1.23 * (i + 1); dt.Rows.Add(dr); } DataView dv = new DataView(dt); return dv; } void Page_Load(Object sender, EventArgs e) { // Load sample data only once, when the page is first loaded. if (!IsPostBack) { ItemsList.DataSource = CreateDataSource(); ItemsList.DataBind(); } } void Item_Command(Object sender, DataListCommandEventArgs e) { // Set the SelectedIndex property to select an item in the DataList. ItemsList.SelectedIndex = e.Item.ItemIndex; // Rebind the data source to the DataList to refresh the control. ItemsList.DataSource = CreateDataSource(); ItemsList.DataBind(); } </script> </head> <body> <form runat="server"> <h3>DataList Select Example</h3> Click <b>Select</b> to select an item. <br><br> <asp:DataList id="ItemsList" GridLines="Both" CellPadding="3" CellSpacing="0" OnItemCommand="Item_Command" runat="server"> <HeaderStyle BackColor="#aaaadd"> </HeaderStyle> <AlternatingItemStyle BackColor="Gainsboro"> </AlternatingItemStyle> <SelectedItemStyle BackColor="Yellow"> </SelectedItemStyle> <HeaderTemplate> Items </HeaderTemplate> <ItemTemplate> <asp:LinkButton id="SelectButton" Text="Select" CommandName="Select" runat="server"/> Item <%# DataBinder.Eval(Container.DataItem, "Item") %> </ItemTemplate> <SelectedItemTemplate> Item: <asp:Label id="ItemLabel" Text='<%# DataBinder.Eval(Container.DataItem, "Item") %>' runat="server"/> <br> Quantity: <asp:Label id="QtyLabel" Text='<%# DataBinder.Eval(Container.DataItem, "Qty") %>' runat="server"/> <br> Price: <asp:Label id="PriceLabel" Text='<%# DataBinder.Eval(Container.DataItem, "Price", "{0:c}") %>' runat="server"/> </SelectedItemTemplate> </asp:DataList> </form> </body> </html>
[C++, JScript] No example is available for C++ or JScript. To view a Visual Basic or C# example, click the Language Filter button
in the upper-left corner of the page.
Requirements
Platforms: Windows 2000, Windows XP Professional, Windows Server 2003 family
See Also
DataList Class | DataList Members | System.Web.UI.WebControls Namespace | SelectedIndex | SelectedItem | SelectedItemTemplate | TableItemStyle | Style