溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點擊 登錄注冊 即表示同意《億速云用戶服務(wù)條款》

Repeater中嵌套使用Repeater的方法

發(fā)布時間:2020-10-19 16:53:48 來源:億速云 閱讀:305 作者:小新 欄目:編程語言

這篇文章主要介紹了Repeater中嵌套使用Repeater的方法,具有一定借鑒價值,需要的朋友可以參考下。希望大家閱讀完這篇文章后大有收獲。下面讓小編帶著大家一起了解一下。

Repeater中嵌套使用Repeater的方法

本例使用 vs.net 2008(C#)編寫。來自 admin10000.com

后臺CS代碼

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!Page.IsPostBack)
            {
                this.dtCategory = GetCategoryTable();
                this.dtProduct = GetProductTable();
                rptCategoryList.DataSource = dtCategory;
                rptCategoryList.DataBind();
            }
        }

        // 準(zhǔn)備一張分類表
        DataTable GetCategoryTable()
        {
            DataTable dt = new DataTable();
            dt.Columns.Add("CategoryId", typeof(int));
            dt.Columns.Add("CategoryTitle", typeof(string));
            for (int i = 1; i <= 3; i++)
            {
                DataRow row = dt.NewRow();
                row["CategoryId"] = i;
                row["CategoryTitle"] = "分類名字 " + i + "";
                dt.Rows.Add(row);
            }
            return dt;
        }
        
        // 準(zhǔn)備一張產(chǎn)品表
        DataTable GetProductTable()
        {
            DataTable dt = new DataTable();
            dt.Columns.Add("ProductTitle", typeof(string));
            dt.Columns.Add("CategoryId", typeof(int));
            for (int i = 1; i <= 9; i++)
            {
                DataRow row = dt.NewRow();
                row["ProductTitle"] = "產(chǎn)品名字 " + i + "";
                if (i > 6) row["CategoryId"] = 3;
                else if (i > 3) row["CategoryId"] = 2;
                else row["CategoryId"] = 1;
                dt.Rows.Add(row);
            }
            return dt;
        }

        // 獲取某個類別的產(chǎn)品
        DataTable GetProductTable(int categoryId)
        {
            DataView dv = this.dtProduct.DefaultView;
            dv.RowFilter = " CategoryId=" + categoryId + " ";
            return dv.ToTable();
        }

        protected void rptCategoryList_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["CategoryTitle"].ToString();
                Repeater rptProductList = (Repeater)e.Item.FindControl("rptProductList");
                rptProductList.DataSource = GetProductTable(Convert.ToInt32(drv["CategoryId"]));
                rptProductList.DataBind();
            }
        }

        protected void rptProductList_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["ProductTitle"].ToString();
            }
        }

前臺aspx代碼

  <form id="form1" runat="server">
    <p>
        <asp:Repeater ID="rptCategoryList" runat="server" OnItemDataBound="rptCategoryList_ItemDataBound">
            <ItemTemplate>
                <p class="listBox">
                    <p class="title">
                        <asp:Literal ID="ltlTitle" runat="server"></asp:Literal></p>
                    <p class="content">
                        <ul>
                            <asp:Repeater ID="rptProductList" runat="server" OnItemDataBound="rptProductList_ItemDataBound">
                                <ItemTemplate>
                                    <li>
                                        <asp:Literal ID="ltlTitle" runat="server"></asp:Literal>
                                    </li>
                                </ItemTemplate>
                            </asp:Repeater>
                        </ul>
                    </p>
                </p>
            </ItemTemplate>
        </asp:Repeater>
    </p>
    </form>

下載代碼示例:Repeater中嵌套使用Repeater的方法PageDemo.RAR

感謝你能夠認(rèn)真閱讀完這篇文章,希望小編分享Repeater中嵌套使用Repeater的方法內(nèi)容對大家有幫助,同時也希望大家多多支持億速云,關(guān)注億速云行業(yè)資訊頻道,遇到問題就找億速云,詳細(xì)的解決方法等著你來學(xué)習(xí)!

向AI問一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進(jìn)行舉報,并提供相關(guān)證據(jù),一經(jīng)查實,將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI