Repeater中巢狀使用Repeater

Web開發者發表於2012-01-28

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

        // 準備一張分類表
        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;
        }
        
        // 準備一張產品表
        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"] = "產品名字 " + i + "";
                if (i > 6) row["CategoryId"] = 3;
                else if (i > 3) row["CategoryId"] = 2;
                else row["CategoryId"] = 1;
                dt.Rows.Add(row);
            }
            return dt;
        }

        // 獲取某個類別的產品
        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">
    <div>
        <asp:Repeater ID="rptCategoryList" runat="server" OnItemDataBound="rptCategoryList_ItemDataBound">
            <ItemTemplate>
                <div class="listBox">
                    <div class="title">
                        <asp:Literal ID="ltlTitle" runat="server"></asp:Literal></div>
                    <div 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>
                    </div>
                </div>
            </ItemTemplate>
        </asp:Repeater>
    </div>
    </form>

下載程式碼示例:Repeater中巢狀使用Repeater PageDemo.RAR

相關文件: Repeater控制元件的分頁實現  Repeater控制元件實現編輯、更新、刪除操作  Repeater多行間隔顯示分隔符的方法

相關文章