Repeater绑定ArrayList数据源

Repeater绑定ArrayList数据源 就相当与把ArrayList的数据绑定到Repeater的ItemTemplate 上 就是RepeaterItem等于ArrayList的一项例如

MessageBox MBx = new MessageBox();     'MessageBox 为一个类
ArrayList lst = new ArrayList();
lst.Add(MBx);
Repeater1.DataSource = lst;
Repeater1.DataBind();

这样 在页面上
<asp:Repeater ID="Repeater1" runat="server" OnItemCommand="Repeater1_ItemCommand" >
        <ItemTemplate>
            <div style="width:200px; height:100px; background-color:Green;"><%#Container.DataItem%>>
            </div>
       </ItemTemplate>
        </asp:Repeater>

就会输出MessageBox 类名称

说明 RepeaterItem就相当于 MBx

 

 

看 MSDN 上讲,可以作为数据绑定控件的数据源对象需要继承并实现 IEnumerable 或  IListSource 接口。
而 Dictionary 字典类很明显是继承并实现了 IEnumerable 接口的,那么如何将一个 Dictionary 对象绑定到 Repeater 等控件呢?

 

Dictionary 绑定如下:

 

ExpandedBlockStart.gif 代码
 Dictionary < string string >  dictionary  =   new  Dictionary < string string > ();
                dictionary.Add(
" 1 " , " 张三 " );
                dictionary.Add(
" 2 " " 李四 " );
                dictionary.Add(
" 3 " " 王五 " );

 

 

 

ExpandedBlockStart.gif 代码
< form  runat ="server" >   
 
< asp:Repeater  id ="List"  runat ="server" >   
     
< HeaderTemplate >   
        
< table  border ="1"  width ="100%" >   
            
< tr >   
                
< th > 站长 </ th >   
                 
< th > 网站 </ th >   
             
</ tr >   
    
</ HeaderTemplate >   
    
< ItemTemplate >   
            
< tr >   
                
< td > <% # Eval ( " key " ) %> </ td >   
                
< td > <% # Eval ( " value " ) %> </ td >   
            
</ tr >   
    
</ ItemTemplate >   
    
< FooterTemplate >   
        
</ table >   
    
</ FooterTemplate >   
</ asp:Repeater >   
</ form >   

 

 

如果使用的是 ArrayList 等类时,绑定时,直接用 <%# Container.DataItem%> 就可以了。

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值