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 属性显示 控件集合中的 DataListItem 对象的 Items 索引号。

注意

下面的代码示例使用单文件代码模型,如果直接复制到代码隐藏文件中,可能无法正常工作。 此代码示例必须复制到扩展名为 .aspx 的空文本文件中。 有关 Web 窗体代码模型的详细信息,请参阅 ASP.NET Web 窗体页代码模型

<%@ 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使用 属性确定集合中DataListItem对象的Items索引号。

注意

此属性仅适用于 控件中的数据 DataList 项。 对象的 ItemType 属性 DataListItem 必须设置为 ListItemType.ItemListItemType.AlternatingItemListItemType.SelectedItemListItemType.EditItem

适用于

另请参阅