如何将Dictionary绑定到Repeater?
看MSDN上讲,可以作为数据绑定控件的数据源对象需要继承并实现IEnumerable或 IListSource接口。
而Dictionary字典类很明显是继承并实现了IEnumerable接口的,那么,如何讲一个Dictionary对象绑定到Repeater等控件呢?
--------------------
下面给出示例(片段)
--------------------
而在前台的使用则是通过数据绑定,如下所示(片段)
------------
如果使用的是ArrayList等类时,
绑定时,直接用 <%# Container.DataItem%>就可以了。。。
而Dictionary字典类很明显是继承并实现了IEnumerable接口的,那么,如何讲一个Dictionary对象绑定到Repeater等控件呢?
--------------------
下面给出示例(片段)
1
public 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
}
:
public class DicTest:Page2
{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>
<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%>就可以了。。。
<h3>
心静似高山流水不动,心清若巫峰雾气不沾。
</h3>
心静似高山流水不动,心清若巫峰雾气不沾。
</h3>



浙公网安备 33010602011771号