TableCellCollection.GetEnumerator 方法的C#例子

使用GetEnumerator创建IEnumerator
本文介绍了一个使用GetEnumerator方法创建实现了System.Collections.IEnumerator接口的对象的例子。通过循环遍历此对象,可以展示表格中选定行的各项元素。
None.gif示例说明如何使用 GetEnumerator 方法创建一个实现了 System.Collections.IEnumerator 的对象。然后循环访问该对象以显示选定行中的项。
None.gif
None.gifview plaincopy to clipboardprint
?
None.gif
<%@ Page Language="C#" AutoEventWireup="True" %>   
None.gif  
None.gif
<HTML>   
None.gif 
<HEAD>   
None.gif    
None.gif    
<SCRIPT runat="server">  
None.gif   
None.gif       
void Page_Load(Object sender, EventArgs e)   
ExpandedBlockStart.gifContractedBlock.gif       
dot.gif{  
InBlock.gif            
InBlock.gif          
int numrows = 5;  
InBlock.gif          
int numcells = 6;  
InBlock.gif          
int counter = 1;  
InBlock.gif          ArrayList a_row 
= new ArrayList();  
InBlock.gif            
InBlock.gif          
// Create a table.  
InBlock.gif
          for (int j=0; j<numrows; j++)   
ExpandedSubBlockStart.gifContractedSubBlock.gif          
dot.gif{            
InBlock.gif             TableRow r 
= new TableRow();  
InBlock.gif             
for (int i=0; i<numcells; i++)   
ExpandedSubBlockStart.gifContractedSubBlock.gif             
dot.gif{  
InBlock.gif                TableCell c 
= new TableCell();  
InBlock.gif                c.Text
=counter.ToString();  
InBlock.gif                r.Cells.Add(c);  
InBlock.gif                counter
++;  
ExpandedSubBlockEnd.gif             }
  
InBlock.gif             Table1.Rows.Add(r);  
ExpandedSubBlockEnd.gif          }
  
InBlock.gif           
InBlock.gif          
if (!IsPostBack)   
ExpandedSubBlockStart.gifContractedSubBlock.gif          
dot.gif{  
InBlock.gif   
InBlock.gif             
// Create a DropDownList for the number of rows.  
InBlock.gif
             for (int k=0; k<numrows; k++)   
ExpandedSubBlockStart.gifContractedSubBlock.gif             
dot.gif{  
InBlock.gif                a_row.Add(k.ToString());  
ExpandedSubBlockEnd.gif             }
  
InBlock.gif           
InBlock.gif             List1.DataSource
=a_row;   
InBlock.gif             List1.DataBind();  
InBlock.gif          
ExpandedSubBlockEnd.gif          }
  
ExpandedBlockEnd.gif       }
  
None.gif   
None.gif    
void Button_Click(object sender, EventArgs e)   
ExpandedBlockStart.gifContractedBlock.gif    
dot.gif{  
InBlock.gif   
InBlock.gif       
int row = List1.SelectedIndex;  
InBlock.gif       TableCell current_cell;  
InBlock.gif   
InBlock.gif       
// Create the IEnumerator.  
InBlock.gif
       IEnumerator myEnum = Table1.Rows[row].Cells.GetEnumerator();        
InBlock.gif   
InBlock.gif       Label1.Text 
= "The items in the selected row are: ";  
InBlock.gif   
InBlock.gif       
// Iterate through the IEnumerator and display its contents.  
InBlock.gif
       while (myEnum.MoveNext())   
ExpandedSubBlockStart.gifContractedSubBlock.gif       
dot.gif{  
InBlock.gif            
InBlock.gif          current_cell 
= (TableCell)myEnum.Current;  
InBlock.gif          Label1.Text 
= Label1.Text + " " + current_cell.Text;  
InBlock.gif   
ExpandedSubBlockEnd.gif       }
  
InBlock.gif               
ExpandedBlockEnd.gif    }
  
None.gif   
None.gif    
</SCRIPT>   
None.gif    
None.gif    
None.gif    
None.gif    
None.gif    
None.gif    
<H3>TableCellCollection Example</H3>   
None.gif    
<FORM runat="server">   
None.gif       
<ASP:TABLE id=Table1 runat="server" />   
None.gif       
<BR><BR>   
None.gif       
<CENTER>   
None.gif          Select a row:   
None.gif          
<BR><BR>   
None.gif          Row:   
None.gif          
<ASP:DROPDOWNLIST id=List1 runat="server" />   
None.gif    
None.gif          
<BR><BR>   
None.gif          
<ASP:BUTTON id=Button1 onclick=Button_Click runat="server" Text="Create IEnumerator" />   
None.gif          
<BR><BR>   
None.gif          
<ASP:LABEL id=Label1 runat="server" />   
None.gif    
None.gif       
</CENTER>   
None.gif    
None.gif    
</FORM>   
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值