DetailsViewRowCollection.GetEnumerator 方法
定义
重要
一些信息与预发行产品相关,相应产品在发行之前可能会进行重大修改。 对于此处提供的信息,Microsoft 不作任何明示或暗示的担保。
返回一个枚举数,该枚举数包含 DetailsViewRow 对象中的所有 DetailsViewRowCollection 对象。
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 的对象,其中包含 DetailsViewRow 内的所有 DetailsViewRowCollection 对象。
实现
示例
下面的代码示例演示如何使用 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 SubmitButton_Click(Object sender, EventArgs e)
{
// Use the Count property to determine whether the
// Rows collection contains any item.
if (ItemDetailsView.Rows.Count > 0)
{
MessageLabel.Text = "The row values are: <br/><br/>";
// Get the enumerator that contains the data rows in the
// DetailsView control.
IEnumerator rowEnumerator = ItemDetailsView.Rows.GetEnumerator();
// Iterate though the enumerator and display the field values.
while (rowEnumerator.MoveNext())
{
DetailsViewRow row = (DetailsViewRow)rowEnumerator.Current;
// Use the Text property to access the value of
// each cell. In this example, the cells in the
// first column (index 0) contains the field names,
// while the cells in the second column (index 1)
// contains the field value.
MessageLabel.Text += row.Cells[0].Text + " = " +
row.Cells[1].Text + "<br/>";
}
}
else
{
MessageLabel.Text = "No items.";
}
}
</script>
<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
<title>DetailsViewRowCollection GetEnumerator Example</title>
</head>
<body>
<form id="form1" runat="server">
<h3>DetailsViewRowCollection GetEnumerator Example</h3>
<asp:detailsview id="ItemDetailsView"
datasourceid="DetailsViewSource"
allowpaging="true"
autogeneraterows="false"
runat="server">
<fields>
<asp:boundfield datafield="CustomerID"
headertext="Customer ID"/>
<asp:boundfield datafield="CompanyName"
headertext="Company Name"/>
<asp:boundfield datafield="Address"
headertext="Address"/>
<asp:boundfield datafield="City"
headertext="City"/>
<asp:boundfield datafield="PostalCode"
headertext="ZIP Code"/>
<asp:boundfield datafield="Country"
headertext="Country"/>
</fields>
</asp:detailsview>
<br/>
<asp:button id="SubmitButton"
text="Display Row Values"
onclick="SubmitButton_Click"
runat="server"/>
<br/><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="DetailsViewSource"
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 SubmitButton_Click(ByVal sender As Object, ByVal e As EventArgs)
' Use the Count property to determine whether the
' Rows collection contains any item.
If ItemDetailsView.Rows.Count > 0 Then
MessageLabel.Text = "The row values are: <br/><br/>"
' Get the enumerator that contains the data rows in the
' DetailsView control.
Dim rowEnumerator As IEnumerator = ItemDetailsView.Rows.GetEnumerator()
' Iterate though the enumerator and display the field values.
While rowEnumerator.MoveNext()
Dim row As DetailsViewRow = CType(rowEnumerator.Current, DetailsViewRow)
' Use the Text property to access the value of
' each cell. In this example, the cells in the
' first column (index 0) contains the field names,
' while the cells in the second column (index 1)
' contains the field value.
MessageLabel.Text &= row.Cells(0).Text & " = " & _
row.Cells(1).Text & "<br/>"
End While
Else
MessageLabel.Text = "No items."
End If
End Sub
</script>
<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
<title>DetailsViewRowCollection GetEnumerator Example</title>
</head>
<body>
<form id="form1" runat="server">
<h3>DetailsViewRowCollection GetEnumerator Example</h3>
<asp:detailsview id="ItemDetailsView"
datasourceid="DetailsViewSource"
allowpaging="true"
autogeneraterows="false"
runat="server">
<fields>
<asp:boundfield datafield="CustomerID"
headertext="Customer ID"/>
<asp:boundfield datafield="CompanyName"
headertext="Company Name"/>
<asp:boundfield datafield="Address"
headertext="Address"/>
<asp:boundfield datafield="City"
headertext="City"/>
<asp:boundfield datafield="PostalCode"
headertext="ZIP Code"/>
<asp:boundfield datafield="Country"
headertext="Country"/>
</fields>
</asp:detailsview>
<br/>
<asp:button id="SubmitButton"
text="Display Row Values"
onclick="SubmitButton_Click"
runat="server"/>
<br/><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="DetailsViewSource"
selectcommand="Select [CustomerID], [CompanyName], [Address],
[City], [PostalCode], [Country] From [Customers]"
connectionstring=
"<%$ ConnectionStrings:NorthWindConnectionString%>"
runat="server"/>
</form>
</body>
</html>
注解
使用此方法获取一个枚举器,该枚举器可通过线性方式循环访问对象中的每个 DetailsViewRowCollection 项。 若要访问枚举器中当前位置的项,请使用 Current 属性。 MoveNext使用 方法移动到集合中的下一项。 若要将枚举器移动到其初始位置,请使用 Reset 方法。