Repeater多行間隔顯示分隔字元的方法。
本例使用 vs.net 2008(C#)編寫後台.CS代碼。來自 admin10000.com
protected void Page_Load(object sender, EventArgs e) { if (!Page.IsPostBack) { rptList.DataSource = GetTable(); rptList.DataBind(); } } protected void rptList_ItemDataBound(object sender, RepeaterItemEventArgs e) { if (e.Item.ItemType == ListItemType.AlternatingItem || e.Item.ItemType == ListItemType.Item) { DataRowView drv = (DataRowView)e.Item.DataItem; Literal ltlTitle = (Literal)e.Item.FindControl("ltlTitle"); ltlTitle.Text = drv.Row["title"].ToString(); if ((e.Item.ItemIndex + 1) % 5 == 0 && (e.Item.ItemIndex + 1) < 15) { System.Web.UI.LiteralControl ul = new LiteralControl("</ul><p class=\"sep\"></p><ul>"); e.Item.Controls.Add(ul); } } } DataTable GetTable() { DataTable dt = new DataTable(); dt.Columns.Add("title", typeof(string)); for (int i = 1; i <= 15; i++) { DataRow row = dt.NewRow(); row["title"] = "這是文章標題 " + i + ""; dt.Rows.Add(row); } return dt; }
前台.aspx代碼
<form id="form1" runat="server"> <p> <asp:Repeater ID="rptList" runat="server" onitemdatabound="rptList_ItemDataBound"> <HeaderTemplate> <ul> </HeaderTemplate> <ItemTemplate> <li> <asp:Literal ID="ltlTitle" runat="server"></asp:Literal></li> </ItemTemplate> <FooterTemplate> </ul></FooterTemplate> </asp:Repeater> </p> </form>
下載程式碼範例:PageDemo.RAR
相關文檔: Repeater控制項的分頁實現 Repeater控制項實現編輯、更新、刪除操作 Repeater中嵌套使用Repeater