repeater绑定数组、哈希表、字典 ArrayList/HashTable,Dictionary为datasource
原文发布时间为:2009-11-19 —— 来源于本人的百度文章 [由搬家工具导入]
repeater绑定数组、哈希表、字典datasource为ArrayList/HashTable,Dictionary时,范例
Default7.aspx 前台页面:
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default7.aspx.cs" Inherits="Default7" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title>http://hi.baidu.com/handboy</title>
</head>
<body>
<form id="form1" runat="server">
<div>
绑定数组
<hr />
<ul>
<asp:Repeater ID="repArray" runat="server">
<ItemTemplate>
<li>
<%#Container.DataItem %>
</li>
</ItemTemplate>
</asp:Repeater>
</ul>
绑定Hashtable
<hr />
<ul>
<asp:Repeater ID="repHash" runat="server">
<ItemTemplate>
<li>
Key:<%#Eval("key") %>,Value:<%#Eval("value") %>
</li>
</ItemTemplate>
</asp:Repeater>
</ul>
</div>
</form>
</body>
</html>
Default7.cs 后台页面:
using System;
using System.Web.UI.WebControls;
using System.Collections;
public partial class Default7 : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
BindArray();//绑定数组
BindHashtable();//绑定哈希表,绑定Dictionary<>跟哈希表一样
}
}
protected void BindArray()
{
ArrayList arr = new ArrayList();
arr.Add("arr11111111111");
arr.Add("arr22222222222");
arr.Add("arr33333333333");
arr.Add("arr44444444444");
repArray.DataSource = arr;
repArray.DataBind();
}
protected void BindHashtable()
{
Hashtable hash = new Hashtable();
hash.Add(11,"hash11111111111");
hash.Add(22,"hash22222222222");
hash.Add(33,"hash33333333333");
hash.Add(44,"hash44444444444");
repHash.DataSource = hash;
repHash.DataBind();
}
}