Repeater에 중첩된 Repeater 사용

巴扎黑
풀어 주다: 2017-04-30 10:19:07
원래의
1784명이 탐색했습니다.

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">
    <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

관련 문서: Repeater 컨트롤의 페이징 구현 Repeater 컨트롤은 편집, 업데이트 및 삭제 작업을 구현합니다. Repeater의 여러 행에 구분 기호를 표시하는 방법

위 내용은 Repeater에 중첩된 Repeater 사용의 상세 내용입니다. 자세한 내용은 PHP 중국어 웹사이트의 기타 관련 기사를 참조하세요!

관련 라벨:
원천:php.cn
본 웹사이트의 성명
본 글의 내용은 네티즌들의 자발적인 기여로 작성되었으며, 저작권은 원저작자에게 있습니다. 본 사이트는 이에 상응하는 법적 책임을 지지 않습니다. 표절이나 침해가 의심되는 콘텐츠를 발견한 경우 admin@php.cn으로 문의하세요.
최신 이슈
인기 튜토리얼
더>
최신 다운로드
더>
웹 효과
웹사이트 소스 코드
웹사이트 자료
프론트엔드 템플릿
회사 소개 부인 성명 Sitemap
PHP 중국어 웹사이트:공공복지 온라인 PHP 교육,PHP 학습자의 빠른 성장을 도와주세요!