共用方式為


DataList.SelectedItem 屬性

定義

取得 DataList 控制項中選取的項目。

public:
 virtual property System::Web::UI::WebControls::DataListItem ^ SelectedItem { System::Web::UI::WebControls::DataListItem ^ get(); };
[System.ComponentModel.Browsable(false)]
public virtual System.Web.UI.WebControls.DataListItem SelectedItem { get; }
[<System.ComponentModel.Browsable(false)>]
member this.SelectedItem : System.Web.UI.WebControls.DataListItem
Public Overridable ReadOnly Property SelectedItem As DataListItem

屬性值

DataListItem,表示 DataList 控制項中選取的項目。

屬性

範例

下列程式代碼範例示範如何使用 SelectedItem 屬性來顯示 控件中 DataList 所選項目的內容。

注意

下列程式代碼範例會使用單一檔案程式代碼模型,如果直接複製到程式代碼後置檔案,可能無法正常運作。 此程式代碼範例必須複製到擴展名為 .aspx的空文本檔。 如需 Web Forms 程式代碼模型的詳細資訊,請參閱 ASP.NET 網頁代碼模型

<%@ Page Language="C#" 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="C#" runat="server">

      public DataTable Cart;
      public DataView CartView;

      void Page_Load(Object sender, EventArgs e) 
      {
         if (Session["DL3_ShoppingCart"] == null) 
         {
            Cart = new DataTable();
            Cart.Columns.Add(new DataColumn("Qty", typeof(string)));
            Cart.Columns.Add(new DataColumn("Item", typeof(string)));
            Cart.Columns.Add(new DataColumn("Price", typeof(string)));
            Session["DL3_ShoppingCart"] = Cart;

            // First Load - Create some data.
            for (int i=1; i<=9; i++) 
            {
               DataRow dr = Cart.NewRow();
               dr[0] = ((i%2)+1).ToString();
               dr[1] = "Item " + i.ToString();
               dr[2] = (1.23 * (i+1)).ToString();
               Cart.Rows.Add(dr);
            }
         }
         else
            Cart = (DataTable)Session["DL3_ShoppingCart"];

         CartView = new DataView(Cart);
         CartView.Sort = "Item";
         if (!IsPostBack)
            BindList();
      }

      void BindList() 
      {
         DataList1.DataSource= CartView;
         DataList1.DataBind();
      }

      void DataList_ItemCommand(Object sender, DataListCommandEventArgs e) 
      {
         DataList1.SelectedIndex = e.Item.ItemIndex;
         BindList();
         Label4.Text = "You selected: "  + 
                       ((Label)DataList1.SelectedItem.FindControl("Label1")).Text;
      }

      void Button_Click(Object sender, EventArgs e)
      { 
         if (DataList1.SelectedItem != null)
         {
            DataList1.SelectedItem.ForeColor = System.Drawing.Color.Red;
         }
      }

   </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"/>
            <%# ((DataRowView)Container.DataItem)["Item"]  %>

         </ItemTemplate>
              
         <SelectedItemTemplate>

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

            <br />

            Quantity:
            <asp:Label id="Label2" 
                 Text='<%# ((DataRowView)Container.DataItem)["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>
<%@ 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>

備註

SelectedItem使用 屬性可取得 DataListItem 物件,代表控件中DataList選取的專案。 然後,您可以使用這個物件來存取所選取項目的屬性。

適用於

另請參閱