首页 > 编程语言 > Repeater多行间隔显示分隔符的方法
2014
12-02

Repeater多行间隔显示分隔符的方法

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><div class=\"sep\"></div><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">
    <div>
     <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>
    </div>
    </form>

下载代码示例: PageDemo.RAR

相关文档: Repeater控件的分页实现  Repeater控件实现编辑、更新、删除操作  Repeater中嵌套使用Repeater

编程技巧