DataKeyArray 类  
定义
重要
一些信息与预发行产品相关,相应产品在发行之前可能会进行重大修改。 对于此处提供的信息,Microsoft 不作任何明示或暗示的担保。
表示 DataKey 对象集合。 此类不能被继承。
public ref class DataKeyArray sealed : System::Collections::ICollection, System::Web::UI::IStateManagerpublic sealed class DataKeyArray : System.Collections.ICollection, System.Web.UI.IStateManagertype DataKeyArray = class
    interface ICollection
    interface IEnumerable
    interface IStateManagerPublic NotInheritable Class DataKeyArray
Implements ICollection, IStateManager- 继承
- 
				DataKeyArray
- 实现
示例
下面的代码示例演示如何使用索引器从DataKeyArray集合中DataKey检索对象。
<%@ Page language="C#" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
    "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<script runat="server">
  void CustomerGridView_DataBound(Object sender, EventArgs e)
  {           
    // Use the indexer to retrieve the DataKey object for the 
    // first record.
    DataKey key = CustomerGridView.DataKeys[0];
    // Display the value of the primary key for the first
    // record displayed in the GridView control.
    MessageLabel.Text = "The primary key of the first record displayed is " +
      key.Value.ToString() + ".";
  }
  void CopyArray_Click(Object sender, EventArgs e)
  {
      DataKeyArray theKeys = CustomerGridView.DataKeys;
      DataKey[] myNewArray = new DataKey[theKeys.Count];
      theKeys.CopyTo(myNewArray, 0);
      Label2.Visible = true;
      // Display first page key values from the new array.
      for (int i = 0; i < myNewArray.Length; i++)
      {
          Label2.Text += "<br />" + myNewArray[i].Value;
      }
  }
</script>
<html  >
  <head id="Head1" runat="server">
    <title>DataKeyArray Example</title>
</head>
<body>
    <form id="form1" runat="server">
      <h3>DataKeyArray Example</h3>
        <asp:gridview id="CustomerGridView"
          datasourceid="CustomerDataSource"
          autogeneratecolumns="true"
          datakeynames="CustomerID"  
          allowpaging="true"
          ondatabound="CustomerGridView_DataBound" 
          runat="server">
        </asp:gridview>
        <br/>
        <asp:label id="MessageLabel"
          forecolor="Red"
          runat="server"/>
        <!-- This example uses Microsoft SQL Server and connects  -->
        <!-- to the Northwind sample database. Use an ASP.NET     -->
        <!-- expression to retrieve the connection string value   -->
        <!-- from the Web.config file.                            -->
        <asp:sqldatasource id="CustomerDataSource"
          selectcommand="Select [CustomerID], [CompanyName], [Address], [City], [PostalCode], [Country] From [Customers]"
          connectionstring="<%$ ConnectionStrings:NorthwindConnectionString %>" 
          runat="server"/>
        <asp:Button ID="CopyArray" 
            runat="server" 
            Text="Copy DataKeyArray to Array" 
            OnClick="CopyArray_Click" />
        <br />
        <asp:label id="Label2"
          runat="server"
          Visible="false"  
          Text="First page of Copied Array Key Values" />
      </form>
  </body>
</html>
<%@ Page language="VB" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
    "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<script runat="server">
  Sub CustomerGridView_DataBound(ByVal sender As Object, ByVal e As EventArgs) Handles CustomerGridView.DataBound
          
    ' Use the indexer to retrieve the DataKey object for the 
    ' first record.
    Dim key As DataKey = CustomerGridView.DataKeys(0)
    ' Display the value of the primary key for the first
    ' record displayed in the GridView control.
    MessageLabel.Text = "The primary key of the first record displayed is " & _
      key.Value.ToString() & "."
  
    End Sub
    
    Sub CopyArray_Click(ByVal sender As Object, ByVal e As EventArgs)
        Dim theKeys As DataKeyArray = CustomerGridView.DataKeys
        Dim myNewArray(theKeys.Count - 1) As DataKey
        theKeys.CopyTo(myNewArray, 0)
        Label2.Visible = True
        ' Display first page key values from the new array.  
        For i As Integer = 0 To myNewArray.Length - 1
            Label2.Text &= "<br />" & myNewArray(i).Value
        Next i
    End Sub
  
</script>
<html xmlns="http://www.w3.org/1999/xhtml" >
  <head id="Head1" runat="server">
    <title>DataKeyArray Example</title>
</head>
<body>
    <form id="form1" runat="server">
        
      <h3>DataKeyArray Example</h3>
                       
        <asp:gridview id="CustomerGridView"
          datasourceid="CustomerDataSource"
          autogeneratecolumns="true"
          datakeynames="CustomerID"  
          allowpaging="true"
          runat="server">
            
        </asp:gridview>
        
        <br/>
        
        <asp:label id="MessageLabel"
          forecolor="Red"
          runat="server"/>
            
        <!-- This example uses Microsoft SQL Server and connects  -->
        <!-- to the Northwind sample database. Use an ASP.NET     -->
        <!-- expression to retrieve the connection string value   -->
        <!-- from the Web.config file.                            -->
        <asp:sqldatasource id="CustomerDataSource"
          selectcommand="Select [CustomerID], [CompanyName], [Address], [City], [PostalCode], [Country] From [Customers]"
          connectionstring="<%$ ConnectionStrings:NorthWindConnectionString%>" 
          runat="server"/>
        <asp:Button ID="CopyArray" 
            runat="server" 
            Text="Copy DataKeyArray to Array" 
            OnClick="CopyArray_Click" />
        <br />
        <asp:label id="Label2"
          runat="server"
          Visible="false"  
          Text="First page of Copied Array Key Values" />
            
      </form>
  </body>
</html>
下面的代码示例演示如何循环访问集合 DataKeyArray 。
<%@ Page language="C#" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
    "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<script runat="server">
  void CustomerGridView_DataBound(Object sender, EventArgs e)
  {    
    // Display the value of the primary key for each
    // record in the GridView control.
    MessageLabel.Text = "The primary key of each record displayed are: <br/><br/>";
    foreach (DataKey key in CustomerGridView.DataKeys)
    {
       MessageLabel.Text += key.Value.ToString() + "<br/>";
    }
  }
  
</script>
<html xmlns="http://www.w3.org/1999/xhtml" >
  <head runat="server">
    <title>DataKeyArray Example</title>
</head>
<body>
    <form id="form1" runat="server">
        
      <h3>DataKeyArray Example</h3>
                       
        <asp:gridview id="CustomerGridView"
          datasourceid="CustomerDataSource"
          autogeneratecolumns="true"
          datakeynames="CustomerID"  
          allowpaging="true"
          ondatabound="CustomerGridView_DataBound" 
          runat="server">
            
        </asp:gridview>
        
        <br/>
        
        <asp:label id="MessageLabel"
          forecolor="Red"
          runat="server"/>
            
        <!-- This example uses Microsoft SQL Server and connects  -->
        <!-- to the Northwind sample database. Use an ASP.NET     -->
        <!-- expression to retrieve the connection string value   -->
        <!-- from the Web.config file.                            -->
        <asp:sqldatasource id="CustomerDataSource"
          selectcommand="Select [CustomerID], [CompanyName], [Address], [City], [PostalCode], [Country] From [Customers]"
          connectionstring="<%$ ConnectionStrings:NorthWindConnectionString%>" 
          runat="server"/>
            
      </form>
  </body>
</html>
<%@ Page language="VB" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
    "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<script runat="server">
  Sub CustomerGridView_DataBound(ByVal sender As Object, ByVal e As EventArgs) Handles CustomerGridView.DataBound
    
    ' Display the value of the primary key for each
    ' record in the GridView control.
    MessageLabel.Text = "The primary key of each record displayed are: <br/><br/>"
    Dim key As DataKey
    For Each key In CustomerGridView.DataKeys
    
      MessageLabel.Text += key.Value.ToString() + "<br/>"
      
    Next
    
  End Sub
  
</script>
<html xmlns="http://www.w3.org/1999/xhtml" >
  <head runat="server">
    <title>DataKeyArray Example</title>
</head>
<body>
    <form id="form1" runat="server">
        
      <h3>DataKeyArray Example</h3>
                       
        <asp:gridview id="CustomerGridView"
          datasourceid="CustomerDataSource"
          autogeneratecolumns="true"
          datakeynames="CustomerID"  
          allowpaging="true"
          runat="server">
            
        </asp:gridview>
        
        <br/>
        
        <asp:label id="MessageLabel"
          forecolor="Red"
          runat="server"/>
            
        <!-- This example uses Microsoft SQL Server and connects  -->
        <!-- to the Northwind sample database. Use an ASP.NET     -->
        <!-- expression to retrieve the connection string value   -->
        <!-- from the Web.config file.                            -->
        <asp:sqldatasource id="CustomerDataSource"
          selectcommand="Select [CustomerID], [CompanyName], [Address], [City], [PostalCode], [Country] From [Customers]"
          connectionstring="<%$ ConnectionStrings:NorthWindConnectionString%>" 
          runat="server"/>
            
      </form>
  </body>
</html>
注解
类 DataKeyArray 用于存储和管理对象的集合 DataKey 。 对象 DataKey 表示数据绑定控件中记录的主键。 通常,显示多个记录 (的数据绑定控件(如 GridView 控件)) 使用 DataKeyArray 对象来存储 DataKey 控件中显示的记录的对象。
类 DataKeyArray 支持通过多种方式访问集合中的项:
- GetEnumerator使用 方法检索可用于循环访问集合的枚举器。 
- CopyTo使用 方法将集合中的项复制到数组中,该数组随后可用于访问集合中的项。 
若要确定集合中的项总数,请使用 Count 属性。
构造函数
| DataKeyArray(ArrayList) | 初始化 DataKeyArray 类的新实例。 | 
属性
| Count | 获取集合中的项数。 | 
| IsReadOnly | 获取一个值,该值指示是否可以修改集合中的项。 | 
| IsSynchronized | 获取指示 DataKeyArray 集合是否已同步(线程安全)的值。 | 
| Item[Int32] | 从集合中的指定的索引处获取 DataKey 对象。 | 
| SyncRoot | 获取用于同步访问集合的对象。 | 
方法
| CopyTo(DataKey[], Int32) | 将此集合中的所有项复制到 DataKey 对象的指定数组(从数组中的指定索引位置开始)。 | 
| Equals(Object) | 确定指定对象是否等于当前对象。(继承自 Object) | 
| GetEnumerator() | 返回一个包含集合中的所有 DataKey 对象的枚举数。 | 
| GetHashCode() | 作为默认哈希函数。(继承自 Object) | 
| GetType() | 获取当前实例的 Type。(继承自 Object) | 
| MemberwiseClone() | 创建当前 Object 的浅表副本。(继承自 Object) | 
| ToString() | 返回表示当前对象的字符串。(继承自 Object) | 
显式接口实现
| ICollection.CopyTo(Array, Int32) | |
| IStateManager.IsTrackingViewState | 获取一个值,该值指示 DataKeyArray 对象是否跟踪其视图状态更改。 | 
| IStateManager.LoadViewState(Object) | 加载以前保存的 DataKeyArray 对象的视图状态。 | 
| IStateManager.SaveViewState() | 保存 DataKeyArray 对象的当前视图状态。 | 
| IStateManager.TrackViewState() | 标记开始跟踪并将视图状态更改保存到 DataKeyArray 对象的起点。 | 
扩展方法
| Cast<TResult>(IEnumerable) | 将 IEnumerable 的元素强制转换为指定的类型。 | 
| OfType<TResult>(IEnumerable) | 根据指定类型筛选 IEnumerable 的元素。 | 
| AsParallel(IEnumerable) | 启用查询的并行化。 | 
| AsQueryable(IEnumerable) | 将 IEnumerable 转换为 IQueryable。 |