在CN.Text中,网站分类需要支持二级分类。我在开发网站分类的后台管理程序时,遇到了嵌套Repeater的问题,写出来给大家参考。
本文主要讲述如何实现在两个嵌套的Repeater之间插入另外的Control。即实现这样的嵌套:
这里的嵌套与一般的Repeater嵌套不同之处就是多了个控件 Advancedpanel1。
对于一般的Repeater嵌套, 可以这样进行数据绑定, 代码如下:
嵌套Repeater的关键是第二个Repeater的数据源的绑定。网上很多文章采用DataSet.Relations进行数据绑定, 参考文章: http://support.microsoft.com/default.aspx?scid=kb;EN-US;306154 。
我这里采用的方法是通过调用codebehind中的GetGlobalCategory,GetGlobalCategory根据ParetntID参数值返回相应的LinkCategoryCollection作为CategoryLevel2的数据源。GetGlobalCategory的代码如下:
当我加入控件Advancedpanel1后, 就会出现错误:Advancedpanel 并不包含对“DataItem”的定义。由于我当时不理解Container的作用,不知如何解决这个问题。在网上也没有找到相应的解决方法。后来,我就直接显示Container.ToString(),发现Container实际上就是它所在的Control的一个引用。在Repeater的ItemTemplate中,Container引用的是RepeaterItem。在加入了Advancedpanel1后, Container引用的是Advancedpanel1,所以会出现错误。要想解决问题,我们需要在DataBinder.Eval(Container.DataItem, "CategoryID")中使Container去引用CategoryLevel1的ItemTemplate。正确的代码应该是这样: DataBinder.Eval(((RepeaterItem)Container.Parent).DataItem, "CategoryID")。完整的代码如下:
BTW: 目前博客园编辑器的编辑区太小,文章长的时候,编辑起来很不方便,要经常上下移动,看来需要支持全屏编辑,我考虑一下,尽快实现这个功能。
#
re: [CN.Text开发笔记]嵌套Repeater的问题
2004-10-01 19:01
wayfarer
#
re: [CN.Text开发笔记]嵌套Repeater的问题
2004-10-01 19:01
wayfarer
本文主要讲述如何实现在两个嵌套的Repeater之间插入另外的Control。即实现这样的嵌套:
<
asp:Repeater
id
="Repeater1"
runat
="server"
>
< ItemTemplate >
< ANW:AdvancedPanel id ="Advancedpanel1" runat ="server" >
< asp:Repeater id ="Repeater2" runat ="server" >
< ItemTemplate >
</ ItemTemplate >
</ asp:Repeater >
</ ANW:AdvancedPanel >
</ ItemTemplate >
</ asp:Repeater >
< ItemTemplate >
< ANW:AdvancedPanel id ="Advancedpanel1" runat ="server" >
< asp:Repeater id ="Repeater2" runat ="server" >
< ItemTemplate >
</ ItemTemplate >
</ asp:Repeater >
</ ANW:AdvancedPanel >
</ ItemTemplate >
</ asp:Repeater >
这里的嵌套与一般的Repeater嵌套不同之处就是多了个控件 Advancedpanel1。
对于一般的Repeater嵌套, 可以这样进行数据绑定, 代码如下:
<
asp:Repeater
id
="CategoryLevel1"
runat
="server"
>
< ItemTemplate >
<%# DataBinder.Eval(Container.DataItem,"Title") %>
<br>
< asp:Repeater id ="CategoryLevel2" runat ="server" DataSource ='<%# GetGlobalCategory(int.Parse(DataBinder.Eval(Container.DataItem, "CategoryID").ToString())) % > '>
< ItemTemplate >
<% # DataBinder.Eval(Container.DataItem,"Title") %>
</ ItemTemplate >
</ asp:Repeater >
</ ANW:AdvancedPanel >
</ ItemTemplate >
</ asp:Repeater >
< ItemTemplate >
<%# DataBinder.Eval(Container.DataItem,"Title") %>
<br>
< asp:Repeater id ="CategoryLevel2" runat ="server" DataSource ='<%# GetGlobalCategory(int.Parse(DataBinder.Eval(Container.DataItem, "CategoryID").ToString())) % > '>
< ItemTemplate >
<% # DataBinder.Eval(Container.DataItem,"Title") %>
</ ItemTemplate >
</ asp:Repeater >
</ ANW:AdvancedPanel >
</ ItemTemplate >
</ asp:Repeater >
嵌套Repeater的关键是第二个Repeater的数据源的绑定。网上很多文章采用DataSet.Relations进行数据绑定, 参考文章: http://support.microsoft.com/default.aspx?scid=kb;EN-US;306154 。
我这里采用的方法是通过调用codebehind中的GetGlobalCategory,GetGlobalCategory根据ParetntID参数值返回相应的LinkCategoryCollection作为CategoryLevel2的数据源。GetGlobalCategory的代码如下:
protected
LinkCategoryCollection GetGlobalCategory(
int
ParentID)
{
return Links.GetCategoriesByType(CategoryType.Global,ParentID,false);
}
{
return Links.GetCategoriesByType(CategoryType.Global,ParentID,false);
}
当我加入控件Advancedpanel1后, 就会出现错误:Advancedpanel 并不包含对“DataItem”的定义。由于我当时不理解Container的作用,不知如何解决这个问题。在网上也没有找到相应的解决方法。后来,我就直接显示Container.ToString(),发现Container实际上就是它所在的Control的一个引用。在Repeater的ItemTemplate中,Container引用的是RepeaterItem。在加入了Advancedpanel1后, Container引用的是Advancedpanel1,所以会出现错误。要想解决问题,我们需要在DataBinder.Eval(Container.DataItem, "CategoryID")中使Container去引用CategoryLevel1的ItemTemplate。正确的代码应该是这样: DataBinder.Eval(((RepeaterItem)Container.Parent).DataItem, "CategoryID")。完整的代码如下:
<
asp:Repeater
id
="CategoryLevel1"
runat
="server"
>
< ItemTemplate >
<%# DataBinder.Eval(Container.DataItem,"Title") %>
<br>
< asp:Repeater id ="CategoryLevel2" runat ="server" DataSource ='<%# GetGlobalCategory(int.Parse(DataBinder.Eval(((RepeaterItem)Container.Parent).DataItem, "CategoryID").ToString())) % > '>
< ItemTemplate >
<% # DataBinder.Eval(Container.DataItem,"Title") %>
</ ItemTemplate >
</ asp:Repeater >
</ ANW:AdvancedPanel >
</ ItemTemplate >
</ asp:Repeater >
< ItemTemplate >
<%# DataBinder.Eval(Container.DataItem,"Title") %>
<br>
< asp:Repeater id ="CategoryLevel2" runat ="server" DataSource ='<%# GetGlobalCategory(int.Parse(DataBinder.Eval(((RepeaterItem)Container.Parent).DataItem, "CategoryID").ToString())) % > '>
< ItemTemplate >
<% # DataBinder.Eval(Container.DataItem,"Title") %>
</ ItemTemplate >
</ asp:Repeater >
</ ANW:AdvancedPanel >
</ ItemTemplate >
</ asp:Repeater >
BTW: 目前博客园编辑器的编辑区太小,文章长的时候,编辑起来很不方便,要经常上下移动,看来需要支持全屏编辑,我考虑一下,尽快实现这个功能。
我现在也越来越喜欢这种方法了,不喜欢使用FindControl给嵌套的Repeater邦定数据
我这里记了两种方法,第一种是用relations第二种是自己写inherit collectionbase的class,最近做的东西用了第二种方法,程序里面一个dataset都没有,直接绑定class,爽
http://www.cnblogs.com/myrat/archive/2004/09/08/40987.aspx
http://www.cnblogs.com/myrat/archive/2004/09/08/40988.aspx
我这里记了两种方法,第一种是用relations第二种是自己写inherit collectionbase的class,最近做的东西用了第二种方法,程序里面一个dataset都没有,直接绑定class,爽
http://www.cnblogs.com/myrat/archive/2004/09/08/40987.aspx
http://www.cnblogs.com/myrat/archive/2004/09/08/40988.aspx
强烈要求楼主贴出全部代码。我看的是懂非懂,本机测试又错误。
我现在也越来越喜欢这种方法了,不喜欢使用FindControl给嵌套的Repeater邦定数据
我这里记了两种方法,第一种是用relations第二种是自己写inherit collectionbase的class,最近做的东西用了第二种方法,程序里面一个dataset都没有,直接绑定class,爽
http://www.cnblogs.com/myrat/archive/2004/09/08/40987.aspx
http://www.cnblogs.com/myrat/archive/2004/09/08/40988.aspx
我这里记了两种方法,第一种是用relations第二种是自己写inherit collectionbase的class,最近做的东西用了第二种方法,程序里面一个dataset都没有,直接绑定class,爽
http://www.cnblogs.com/myrat/archive/2004/09/08/40987.aspx
http://www.cnblogs.com/myrat/archive/2004/09/08/40988.aspx
强烈要求楼主贴出全部代码。我看的是懂非懂,本机测试又错误。
我现在也越来越喜欢这种方法了,不喜欢使用FindControl给嵌套的Repeater邦定数据
我这里记了两种方法,第一种是用relations第二种是自己写inherit collectionbase的class,最近做的东西用了第二种方法,程序里面一个dataset都没有,直接绑定class,爽
http://www.cnblogs.com/myrat/archive/2004/09/08/40987.aspx
http://www.cnblogs.com/myrat/archive/2004/09/08/40988.aspx
我这里记了两种方法,第一种是用relations第二种是自己写inherit collectionbase的class,最近做的东西用了第二种方法,程序里面一个dataset都没有,直接绑定class,爽
http://www.cnblogs.com/myrat/archive/2004/09/08/40987.aspx
http://www.cnblogs.com/myrat/archive/2004/09/08/40988.aspx
强烈要求楼主贴出全部代码。我看的是懂非懂,本机测试又错误。
Feedback
如果无法获得控件本身的Item,则通过其Parent控件来获得,嗯,办法很好,值得借鉴。