Repeater绑定ArrayList数据源

在页面上
<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 绑定如下:


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







代码
<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%> 就可以了。

posted @ 2012-05-17 09:37  念余温  阅读(2171)  评论(0编辑  收藏  举报