如何将Dictionary绑定到Repeater?
生活随笔
收集整理的這篇文章主要介紹了
如何将Dictionary绑定到Repeater?
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
看MSDN上講,可以作為數據綁定控件的數據源對象需要繼承并實現IEnumerable或 IListSource接口。
而Dictionary字典類很明顯是繼承并實現了IEnumerable接口的,那么,如何講一個Dictionary對象綁定到Repeater等控件呢?
--------------------
下面給出示例(片段) ?1public?class?DicTest:Page
?2{
?3??protected?Repeater?nameList;
?4??Dictionary<string,string>?listSource=new?Dictionary<string,string>();
?5??public?DicTest()
?6??{
?7????listSource.Add("Mc1","McJeremy");
?8????listSource.Add("Mc2","McJeremy");
?9????listSource.Add("Mc3","McJeremy");
10????listSource.Add("Mc4","McJeremy");
11??}
12??private?void?Page_Load(object?sender,?EventArgs?e)
13??{
14????nameList.DataSource=listSource;
15????nameList.DataBind();
16??}
17??override?protected?void?OnInit(EventArgs?e)
18??{
19????Initialize();
20????base.OnInit(e);
21??}
22??private?void?Initialize()
23??{
24?????this.Load+=new?EventHandler(this.Page_Load);
25??}
26}:
--------------------
而在前臺的使用則是通過數據綁定,如下所示(片段)
?1<table>
?2<asp:Repeater?id="nameList"?runat="server">
?3??<ItemTemplate>
?4????<tr>
?5?????<td>鍵:<%#Eval("key")%></td>
?6?????<td>值:<%#Eval("value")%></td>
?7????</tr>
?8??</ItemTemplate>
?9</asp:Repeater>
10</table>
------------
如果使用的是ArrayList等類時,
綁定時,直接用 <%# Container.DataItem%>就可以了。。。
而Dictionary字典類很明顯是繼承并實現了IEnumerable接口的,那么,如何講一個Dictionary對象綁定到Repeater等控件呢?
--------------------
下面給出示例(片段) ?1public?class?DicTest:Page
?2{
?3??protected?Repeater?nameList;
?4??Dictionary<string,string>?listSource=new?Dictionary<string,string>();
?5??public?DicTest()
?6??{
?7????listSource.Add("Mc1","McJeremy");
?8????listSource.Add("Mc2","McJeremy");
?9????listSource.Add("Mc3","McJeremy");
10????listSource.Add("Mc4","McJeremy");
11??}
12??private?void?Page_Load(object?sender,?EventArgs?e)
13??{
14????nameList.DataSource=listSource;
15????nameList.DataBind();
16??}
17??override?protected?void?OnInit(EventArgs?e)
18??{
19????Initialize();
20????base.OnInit(e);
21??}
22??private?void?Initialize()
23??{
24?????this.Load+=new?EventHandler(this.Page_Load);
25??}
26}:
--------------------
而在前臺的使用則是通過數據綁定,如下所示(片段)
?1<table>
?2<asp:Repeater?id="nameList"?runat="server">
?3??<ItemTemplate>
?4????<tr>
?5?????<td>鍵:<%#Eval("key")%></td>
?6?????<td>值:<%#Eval("value")%></td>
?7????</tr>
?8??</ItemTemplate>
?9</asp:Repeater>
10</table>
------------
如果使用的是ArrayList等類時,
綁定時,直接用 <%# Container.DataItem%>就可以了。。。
轉載于:https://www.cnblogs.com/McJeremy/archive/2008/05/20/1203021.html
總結
以上是生活随笔為你收集整理的如何将Dictionary绑定到Repeater?的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: linux小窍门──设置时钟和时间
- 下一篇: outlook 发送邮件!