技术 - ASP/.Net - 如何将 ArrayList, Dictionary 绑定到 Repeater?

29
09 Mar.

如何将 ArrayList, Dictionary 绑定到 Repeater?

最后更新: 2009/03/29  |  评论: 0  |  关键词: ArrayList  Dictionary  Repeater  绑定  

看 MSDN 上讲,可以作为数据绑定控件的数据源对象需要继承并实现 IEnumerable 或  IListSource 接口。

而 Dictionary 字典类很明显是继承并实现了 IEnumerable 接口的,那么如何将一个 Dictionary 对象绑定到 Repeater 等控件呢?

  1. Partial Class Repeater_Dictionary_Test  
  2.     Inherits System.Web.UI.Page  
  3.  
  4.     Private SourceDictionary As New Dictionary(Of StringString)()  
  5.  
  6.     Protected Sub Page_Load(ByVal sender As ObjectByVal e As System.EventArgs) Handles Me.Load  
  7.         SourceDictionary.Add("木炭""http://www.woodcoal.cn/")  
  8.         SourceDictionary.Add("无尘""http://www.ekv.cn/")  
  9.         SourceDictionary.Add("可乐·婷""http://www.colastop.cn/")  
  10.         SourceDictionary.Add("牛档""http://www.niudown.com/")  
  11.  
  12.         showRepeater.DataSource = SourceDictionary  
  13.         showRepeater.DataBind()  
  14.     End Sub 
  15. End Class 

而在前台的使用则是通过数据绑定,如下所示(片段)

  1. <form runat="server"> 
  2. <asp:Repeater id="List" runat="server"> 
  3.     <HeaderTemplate> 
  4.         <table border="1" width="100%"> 
  5.             <tr> 
  6.                 <th>站长</th> 
  7.                 <th>网站</th> 
  8.             </tr> 
  9.     </HeaderTemplate> 
  10.     <ItemTemplate> 
  11.             <tr> 
  12.                 <td><%#Eval("key")%></td> 
  13.                 <td><%#Eval("value")%></td> 
  14.             </tr> 
  15.     </ItemTemplate> 
  16.     <FooterTemplate> 
  17.         </table> 
  18.     </FooterTemplate> 
  19. </asp:Repeater> 
  20. </form> 

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

· 本文由 木炭 发布在《激情燃烧的木炭》 上,原文地址为:http://www.woodcoal.cn/technology/asp/2009329-23140-518.html(转载请保留本信息、全文内容和链接)

发表评论

已经有 0 位朋友发表了对《如何将 ArrayList, Dictionary 绑定到 Repeater?》的看法
 
登录名:  密码:   登录  注册
评论: 
User:
Contact:
验证码:  
  [Ctrl+Enter]

相关文章