DataKeyArray.GetEnumerator 方法
定义
重要
一些信息与预发行产品相关,相应产品在发行之前可能会进行重大修改。 对于此处提供的信息,Microsoft 不作任何明示或暗示的担保。
返回一个包含集合中的所有 DataKey 对象的枚举数。
public:
virtual System::Collections::IEnumerator ^ GetEnumerator();
public System.Collections.IEnumerator GetEnumerator ();
abstract member GetEnumerator : unit -> System.Collections.IEnumerator
override this.GetEnumerator : unit -> System.Collections.IEnumerator
Public Function GetEnumerator () As IEnumerator
返回
一个实现了 IEnumerator 的对象,该对象包含集合中的所有 DataKey 对象。
实现
示例
下面的代码示例演示如何循环访问该方法 GetEnumerator 创建的枚举器。
<%@ 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 Count property to determine whether the
// DataKeys collection contains any items.
if (CustomerGridView.DataKeys.Count > 0)
{
MessageLabel.Text = "The primary key of each record displayed are: <br/><br/>";
// Use the GetEnumerator method to create an enumerator that
// contains the DataKey objects for the GridView control.
IEnumerator keyEnumerator = CustomerGridView.DataKeys.GetEnumerator();
// Iterate though the enumerator and display the primary key
// value of each record displayed.
while (keyEnumerator.MoveNext())
{
DataKey key = (DataKey)keyEnumerator.Current;
MessageLabel.Text += key.Value.ToString() + "<br/>";
}
}
else
{
MessageLabel.Text = "No DataKey objects.";
}
}
</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
' Use the Count property to determine whether the
' DataKeys collection contains any items.
If CustomerGridView.DataKeys.Count > 0 Then
MessageLabel.Text = "The primary key of each record displayed are: <br/><br/>"
' Use the GetEnumerator method to create an enumerator that
' contains the DataKey objects for the GridView control.
Dim keyEnumerator As IEnumerator = CustomerGridView.DataKeys.GetEnumerator()
' Iterate though the enumerator and display the primary key
' value of each record displayed.
While keyEnumerator.MoveNext()
Dim key As DataKey = CType(keyEnumerator.Current, DataKey)
MessageLabel.Text &= key.Value.ToString() & "<br/>"
End While
Else
MessageLabel.Text = "No DataKey objects."
End If
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>
注解
使用该方法 GetEnumerator 获取一个枚举器,该枚举器可通过线性方式循环访问集合中的每个项。 若要访问枚举器中当前位置的项,请使用 IEnumerator.Current 该属性。 使用该方法 IEnumerator.MoveNext 移动到集合中的下一项。 若要将枚举器移动到其初始位置,请使用 IEnumerator.Reset 该方法。
备注
最初获取枚举器或使用 IEnumerator.Reset 方法将枚举器移动到集合中的第一项时,必须调用 IEnumerator.MoveNext 该方法。 否则,由属性表示的 IEnumerator.Current 项是未定义的。