Repeater嵌套绑定数据 实现大类下的小类

本文介绍如何在ASP.NET中使用Repeater控件实现嵌套数据绑定,并提供了两种实现方式:一种是在前台页面直接设置数据源,另一种是在ItemDataBound事件中进行数据绑定。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

代码
 <asp:Repeater ID="RepSort" runat="server">
            
<ItemTemplate>
                
<h3 style="background: url(images/bag.jpg) no-repeat;">
                    
<a href='/<%# DataBinder.Eval(Container.DataItem, "SortName")%>.html'>
                        
<%# DataBinder.Eval(Container.DataItem, "SortName")%>
                    
</a>
                
</h3>
                
<div class="left_list">
                    
<ul>
                        
<asp:Repeater ID="RepType" DataSource='<%# ((DataRowView)Container.DataItem).Row.GetChildRows("myrelation") %>'
                            runat
="server">
                            
<ItemTemplate>
                                
<li><a href="bag/mobilebag.htm">
                                    
<%# ((DataRow)Container.DataItem)["TypeName"]%>
                                
</a></li>
                            
</ItemTemplate>
                        
</asp:Repeater>
                    
</ul>
                
</div>
            
</ItemTemplate>
        
</asp:Repeater>

后台:

代码
 public void BindSort()
    {
        SqlConnection conn 
= DB.Getconn();
      

        DataSet ds 
= new DataSet();
        
// 大类
        string sql1 = "select * from dbo.lvrolex_Sort order by paixu";

        SqlDataAdapter sda1 
= new SqlDataAdapter(sql1, conn);
        sda1.Fill(ds, 
"parent");
        
//小类 
        string sql2 = "select * from dbo.lvrolex_Type order by paixu";
        SqlDataAdapter sda2 
= new SqlDataAdapter(sql2, conn);
        sda2.Fill(ds, 
"child");

        
//建立父子关系
        ds.Relations.Add("myrelation", ds.Tables[0].Columns[0], ds.Tables[1].Columns[4]);
        
this.RepSort.DataSource = ds.Tables["parent"].DefaultView;
        
this.RepSort.DataBind();



    }

 

也可以在外层repeater中的ItemDataBound中绑定嵌套的repeater

代码
private void Repparent_ItemDataBound(object sender, System.Web.UI.WebControls.RepeaterItemEventArgs e)
           {
                 
if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)
                 {
                       
                       
                             
string sql2="select Item_Name,Item_Sort_Two from Item_Show where Cname='"+((Label)e.Item.FindControl("Label2")).Text+"' order by Item_Sort_Two";
                             SqlDataAdapter adapter
=ConnClass.GetSqlDataAdapter(sql2);
                             DataSet ds2
=new DataSet();
                             adapter.Fill(ds2,
"Item_Show");
                       
                  
                             ((Repeater)e.Item.FindControl(
"Repeater2")).DataSource=ds2.Tables["Item_Show"].DefaultView;
                             ((Repeater)e.Item.FindControl(
"Repeater2")).DataBind();
                           
                       
                 }
           }
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值