首页 > 代码库 > Repeater嵌套绑定Repeater

Repeater嵌套绑定Repeater

<asp:Repeater runat="server" ID="rpList" OnItemDataBound="rpList_ItemDataBound">
            <ItemTemplate>
                <div class="catItem">
                    <h2>
                        <%#Eval("DataValue")%> </h2>
                    <div class="catType">
                        <asp:Repeater runat="server" ID="rpListSub">
                            <ItemTemplate>
                                <a href=http://www.mamicode.com/"catgory2.aspx?id=">>

cs文件:

protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                InitData();
            }
        }
        public void InitData()
        {
            DataTable dt = SysData.GetExpertiseCategoryWeb();//web项目大类
            this.rpList.DataSource = dt;
            this.rpList.DataBind();
        }
        protected void rpList_ItemDataBound(object sender, RepeaterItemEventArgs e)
        {
            if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)
            {
                Repeater rep = e.Item.FindControl("rpListSub") as Repeater;//找到里层的repeater对象
                DataRowView rowv = (DataRowView)e.Item.DataItem;//找到分类Repeater关联的数据项
                int category = Convert.ToInt32(rowv["DataKey"]);//获取填充子类的id 
                rep.DataSource = SysData.GetExpertiseLevelDTByCategory(category);
                rep.DataBind();
            }
        }