Repeater嵌套的两种方案

http://tmouse.cnblogs.com/archive/2006/05/28/256274.html

 

http://support.microsoft.com/default.aspx?scid=kb;en-us;306154#top

 

在做“主页大巴”项目的时候,现有两次遇到要用到Repeater嵌套,但方法不同,如下:

首先是各个科组教师的主页要分类显示,这个好办,因为用户都是按科组申请注册的,只要他的主页类别是个人主页,就可以做好父子绑定GroupID

程序参考:http://support.microsoft.com/default.aspx?scid=kb;en-us;306154

其二是在个人主页里按栏目分类显示文章时,按理父子关系该是文章的ClassID的父目录是栏目的ClassID,但是有的栏目并没有2级目录,所以文章直接添加在它上面,这样前面的父子关系就不成立,会出现“不能启用此约束,因为不是所有的值都具有相应的父值”的错误报告,后来按这个错误搜索,最后找到CSDN里的这个帖子,csdn的saucer(思归) ( 五星(高级))老大给了解决方法,我修改了一下,应用到我的程序中。

程序参考:http://community.csdn.net/Expert/topic/4229/4229787.xml?temp=.8814051

我修改的程序如下:
aspx中:

< asp:Repeater id = " repLM "  runat = " server " >
    
< ItemTemplate >
        
< table border = " 0 "  style = " BORDER-COLLAPSE: collapse "  width = " 98% "  id = " table8 "   class = " list " >
            
< tr >
                
< td colspan = " 2 " >
                    
< div align = " center " >
                        
< table border = " 0 "  style = " BORDER-COLLAPSE: collapse "  width = " 100% "  id = " table9 " >
                            
< tr >
                                
< td  class = " listTop " >& nbsp; < img src = " <%=strStylePath%>dot2.gif " >& nbsp; <% # DataBinder.Eval(Container.DataItem, " ClassName " %></ td >
                            
</ tr >
                            
< tr >
                                
< td >
                                    
< table border = " 0 "  style = " border-collapse: collapse "  width = " 100% "  id = " table10 " >
                                        
< asp:Repeater id = " repWZ "  runat = " server " >
                                            
< ItemTemplate >
                                                
< tr >
                                                    
< td >< img border = " 0 "  src = " <%=strStylePath%>list.gif "  width = " 15 "  height = " 6 " >< a href = ' Show.aspx?AtcID=<%# DataBinder.Eval(Container.DataItem,"AtcID") %> ' >< font color = ' <%# DataBinder.Eval(Container.DataItem,"TitleColor") %> ' ><% # DataBinder.Eval(Container.DataItem, " Title " %></ font ></ a >
                                                    
</ td >
                                                
</ tr >
                                            
</ ItemTemplate >
                                        
</ asp:Repeater >
                                    
</ table >
                                
</ td >
                            
</ tr >
                        
</ table >
                    
</ div >
                
</ td >
            
</ tr >
            
< tr >
                
< td width = " 87% " >
                
</ td >
                
< td width = " 12% "   class = " more " >< span lang = " zh-cn " > 更多 </ span ></ td >
            
</ tr >
        
</ table >
     </ ItemTemplate >
</ asp:Repeater >


cs中的程序如下:

protected  System.Web.UI.WebControls.Repeater repLM;
protected  System.Web.UI.WebControls.Repeater repWZ;

 

// 先邦定栏目
string  strSQL1 = " select ClassID,ClassName from xClass where ParentID=0 and PageID= " + strPageID;
SqlDataAdapter da1
= new  SqlDataAdapter(strSQL1,myConn);
da1.Fill(ds,
" LM " );
repLM.DataSource
= ds.Tables[ " LM " ].DefaultView;
repLM.DataBind();        
myConn.Close();

在外面的Repeater的ItemDataBound事件里写如下程序:

private   void  repLM_ItemDataBound( object  sender, System.Web.UI.WebControls.RepeaterItemEventArgs e)
{
    
    
if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem) 
    

        Repeater rpColumnNews 
= (Repeater) e.Item.FindControl("repWZ"); 
        
//找到分类Repeater关联的数据项 
        DataRowView rowv = (DataRowView)e.Item.DataItem; 
        
//提取分类ID 
        string strClassID = Convert.ToString(rowv["ClassID"]); 
        
//里面的Repeater
        string strSQL2="select xArticle.AtcID,xArticle.Title,xArticle.TitleColor from xArticle,xClass where ";
        strSQL2
+=" (xArticle.ClassID=xClass.ClassID and xClass.ClassID="+strClassID+") or (xArticle.ClassID=xClass.ClassID and xClass.ParentID="+strClassID+")";
        SqlDataAdapter da2
=new SqlDataAdapter(strSQL2,myConn);
        ds
=new DataSet();
        da2.Fill(ds,
"WZ");
        rpColumnNews.DataSource
=ds.Tables["WZ"].DefaultView;
        rpColumnNews.DataBind();        
        myConn.Close();
    }
 

}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值