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(); } } |