下面的示例展示如何使用 GetEnumerator 方法创建一个实现了 System.Collections.IEnumerator 的对象,该对象被循环访问以显示表中的项。
view plaincopy to clipboardprint ?
<% @ Page Language = " C# " AutoEventWireup = " True " %>
< HTML >
< HEAD >
< SCRIPT runat = " server " >
void Page_Load(Object sender, EventArgs e) {
int numrows = 5;
int numcells = 6;
int counter = 1;
ArrayList a_row = new ArrayList();
// Create a table.
for (int j=0; j<numrows; j++) {
TableRow r = new TableRow();
for (int i=0; i<numcells; i++) {
TableCell c = new TableCell();
c.Text=counter.ToString();
r.Cells.Add(c);
counter++;
}
Table1.Rows.Add(r);
}
}
void Button_Click( object sender, EventArgs e) {
int row_counter = 0;
TableRow current_row;
TableCell current_cell;
// Create an IEnumerator for the rows of a table.
IEnumerator myRowEnum = Table1.Rows.GetEnumerator();
Label1.Text = "The copied items from the table are: ";
// Iterate through the IEnumerator and display its contents.
while (myRowEnum.MoveNext()) {
// Create an IEnumerator for the cells of the row.
IEnumerator myCellEnum = Table1.Rows[row_counter].Cells.GetEnumerator();
// Iterate through the IEnumerator and display its contents.
while (myCellEnum.MoveNext()) {
current_cell = (TableCell)myCellEnum.Current;
Label1.Text = Label1.Text + " " + current_cell.Text;
}
row_counter++;
}
}
</ SCRIPT >
< H3 > TableCellCollection Example </ H3 >
< FORM runat = " server " >
< ASP:TABLE id = Table1 runat = " server " />
< BR >< BR >
< CENTER >
< ASP:BUTTON id = Button1 onclick = Button_Click runat = " server " Text = " Copy Table to Array " />
< BR >< BR >
< ASP:LABEL id = Label1 runat = " server " />
</ CENTER >
</ FORM >
view plaincopy to clipboardprint ?
<% @ Page Language = " C# " AutoEventWireup = " True " %>
< HTML >
< HEAD >
< SCRIPT runat = " server " >
void Page_Load(Object sender, EventArgs e) {
int numrows = 5;
int numcells = 6;
int counter = 1;
ArrayList a_row = new ArrayList();
// Create a table.
for (int j=0; j<numrows; j++) {
TableRow r = new TableRow();
for (int i=0; i<numcells; i++) {
TableCell c = new TableCell();
c.Text=counter.ToString();
r.Cells.Add(c);
counter++;
}
Table1.Rows.Add(r);
}
}
void Button_Click( object sender, EventArgs e) {
int row_counter = 0;
TableRow current_row;
TableCell current_cell;
// Create an IEnumerator for the rows of a table.
IEnumerator myRowEnum = Table1.Rows.GetEnumerator();
Label1.Text = "The copied items from the table are: ";
// Iterate through the IEnumerator and display its contents.
while (myRowEnum.MoveNext()) {
// Create an IEnumerator for the cells of the row.
IEnumerator myCellEnum = Table1.Rows[row_counter].Cells.GetEnumerator();
// Iterate through the IEnumerator and display its contents.
while (myCellEnum.MoveNext()) {
current_cell = (TableCell)myCellEnum.Current;
Label1.Text = Label1.Text + " " + current_cell.Text;
}
row_counter++;
}
}
</ SCRIPT >
< H3 > TableCellCollection Example </ H3 >
< FORM runat = " server " >
< ASP:TABLE id = Table1 runat = " server " />
< BR >< BR >
< CENTER >
< ASP:BUTTON id = Button1 onclick = Button_Click runat = " server " Text = " Copy Table to Array " />
< BR >< BR >
< ASP:LABEL id = Label1 runat = " server " />
</ CENTER >
</ FORM >