示例说明如何使用 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);
}
if (!IsPostBack)
{
// Create a DropDownList for the number of rows.
for (int k=0; k<numrows; k++)
{
a_row.Add(k.ToString());
}
List1.DataSource=a_row;
List1.DataBind();
}
}
void Button_Click( object sender, EventArgs e)
{
int row = List1.SelectedIndex;
TableCell current_cell;
// Create the IEnumerator.
IEnumerator myEnum = Table1.Rows[row].Cells.GetEnumerator();
Label1.Text = "The items in the selected row are: ";
// Iterate through the IEnumerator and display its contents.
while (myEnum.MoveNext())
{
current_cell = (TableCell)myEnum.Current;
Label1.Text = Label1.Text + " " + current_cell.Text;
}
}
</ SCRIPT >
< H3 > TableCellCollection Example </ H3 >
< FORM runat = " server " >
< ASP:TABLE id = Table1 runat = " server " />
< BR >< BR >
< CENTER >
Select a row:
< BR >< BR >
Row:
< ASP:DROPDOWNLIST id = List1 runat = " server " />
< BR >< BR >
< ASP:BUTTON id = Button1 onclick = Button_Click runat = " server " Text = " Create IEnumerator " />
< 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);
}
if (!IsPostBack)
{
// Create a DropDownList for the number of rows.
for (int k=0; k<numrows; k++)
{
a_row.Add(k.ToString());
}
List1.DataSource=a_row;
List1.DataBind();
}
}
void Button_Click( object sender, EventArgs e)
{
int row = List1.SelectedIndex;
TableCell current_cell;
// Create the IEnumerator.
IEnumerator myEnum = Table1.Rows[row].Cells.GetEnumerator();
Label1.Text = "The items in the selected row are: ";
// Iterate through the IEnumerator and display its contents.
while (myEnum.MoveNext())
{
current_cell = (TableCell)myEnum.Current;
Label1.Text = Label1.Text + " " + current_cell.Text;
}
}
</ SCRIPT >
< H3 > TableCellCollection Example </ H3 >
< FORM runat = " server " >
< ASP:TABLE id = Table1 runat = " server " />
< BR >< BR >
< CENTER >
Select a row:
< BR >< BR >
Row:
< ASP:DROPDOWNLIST id = List1 runat = " server " />
< BR >< BR >
< ASP:BUTTON id = Button1 onclick = Button_Click runat = " server " Text = " Create IEnumerator " />
< BR >< BR >
< ASP:LABEL id = Label1 runat = " server " />
</ CENTER >
</ FORM >