共用方式為


DataListItem.ItemIndex 屬性

定義

從控制項的 DataListItem 集合中,取得 Items 物件的索引。

public:
 virtual property int ItemIndex { int get(); };
public virtual int ItemIndex { get; }
member this.ItemIndex : int
Public Overridable ReadOnly Property ItemIndex As Integer

屬性值

DataListItem 集合中 Items 物件的索引。

範例

下列範例示範如何使用 ItemIndex 屬性,顯示控件Items集合中物件的索引編號DataListItem

注意

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

<%@ 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" >
   <script language = "C#" runat="server">
 
      ICollection CreateDataSource() 
      {
         DataTable dt = new DataTable();
         DataRow dr;
 
         dt.Columns.Add(new DataColumn("StringValue", typeof(string)));
  
         for (int i = 0; i < 10; i++) 
         {
            dr = dt.NewRow();
            dr[0] = "Item " + i.ToString();
            dt.Rows.Add(dr);
         }
 
         DataView dv = new DataView(dt);
         return dv;
      }
 
      void Page_Load(Object sender, EventArgs e) 
      {
         if (!IsPostBack) 
         {
            DataList1.DataSource = CreateDataSource();
            DataList1.DataBind();
         }
      }
 
      void Button_Click(Object sender, EventArgs e)
      {

         Label1.Text = "The ItemIndex of each item in the DataList are: <br />"; 
 
         foreach (DataListItem item in DataList1.Items)
         { 
            Label1.Text += "<br />" + item.ItemIndex.ToString() + " - " +
                           ((DataBoundLiteralControl)item.Controls[1]).Text;
         }

      }
  
 
   </script>
 
<head runat="server">
    <title>DataListItem ItemIndex Example</title>
</head>
<body>
 
   <form id="form1" runat="server">

      <h3>DataListItem ItemIndex Example</h3>
 
      <asp:DataList id="DataList1" runat="server"
           BorderColor="black"
           CellPadding="3"
           Font-Names="Verdana"
           Font-Size="8pt">

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

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

         <HeaderTemplate>

            Items

         </HeaderTemplate>
               
         <ItemTemplate>

            <%# DataBinder.Eval(Container.DataItem, "StringValue") %>

         </ItemTemplate>
 
      </asp:DataList>

        <br /><br />

      <asp:Button id="Button1"
           Text="Display ItemIndex for Items in the DataList"
           OnClick="Button_Click"
           runat="server"/>
 
      <br /><br />

      <asp:Label id="Label1"
           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" >
   <script language = "VB" runat="server">
 
    Function CreateDataSource() As ICollection
        Dim dt As New DataTable()
        Dim dr As DataRow
        
        dt.Columns.Add(New DataColumn("StringValue", GetType(String)))
        
        Dim i As Integer
        For i = 0 To 9
            dr = dt.NewRow()
            dr(0) = "Item " & i.ToString()
            dt.Rows.Add(dr)
        Next i
        
        Dim dv As New DataView(dt)
        Return dv
    End Function 'CreateDataSource


    Sub Page_Load(sender As Object, e As EventArgs)
        If Not IsPostBack Then
            DataList1.DataSource = CreateDataSource()
            DataList1.DataBind()
        End If
    End Sub 'Page_Load


    Sub Button_Click(sender As Object, e As EventArgs)
        
        Label1.Text = "The ItemIndex of each item in the DataList are: <br />"
        
        Dim item As DataListItem
        For Each item In  DataList1.Items
            Label1.Text &= "<br />" & item.ItemIndex.ToString() & " - " & CType(item.Controls(1), DataBoundLiteralControl).Text
        Next item
    End Sub 'Button_Click
  
   </script>
 
<head runat="server">
    <title>DataListItem ItemIndex Example</title>
</head>
<body>
 
   <form id="form1" runat="server">

      <h3>DataListItem ItemIndex Example</h3>
 
      <asp:DataList id="DataList1" runat="server"
           BorderColor="black"
           CellPadding="3"
           Font-Names="Verdana"
           Font-Size="8pt">

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

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

         <HeaderTemplate>

            Items

         </HeaderTemplate>
               
         <ItemTemplate>

            <%# DataBinder.Eval(Container.DataItem, "StringValue") %>

         </ItemTemplate>
 
      </asp:DataList>

        <br /><br />

      <asp:Button id="Button1"
           Text="Display ItemIndex for Items in the DataList"
           OnClick="Button_Click"
           runat="server"/>
 
      <br /><br />

      <asp:Label id="Label1"
           runat="server"/>
 
   </form>
 
</body>
</html>

備註

ItemIndex使用 屬性來判斷集合中DataListItemItems物件的索引編號。

注意

此屬性只適用於控件中的數據 DataList 項。 物件的 ItemType 屬性 DataListItem 必須設定為 ListItemType.ItemListItemType.AlternatingItemListItemType.SelectedItemListItemType.EditItem

適用於

另請參閱