在做“主页大巴”项目的时候,现有两次遇到要用到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();
}
}
http://hi.baidu.com/tangfu159/blog/item/d047edfab741042b4e4aea7b.html