路-在继续

MicroCode
  博客园  :: 首页  :: 新随笔  :: 联系 :: 订阅 订阅  :: 管理

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

Posted on 2010-10-30 12:32  路-在继续  阅读(322)  评论(0编辑  收藏  举报
代码
 <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();
                           
                       
                 }
           }