[CN.Text开发笔记]嵌套Repeater的问题
在CN.Text中,网站分类需要支持二级分类。我在开发网站分类的后台管理程序时,遇到了嵌套Repeater的问题,写出来给大家参考。
本文主要讲述如何实现在两个嵌套的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>
这里的嵌套与一般的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>
嵌套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);
}
当我加入控件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>
BTW: 目前博客园编辑器的编辑区太小,文章长的时候,编辑起来很不方便,要经常上下移动,看来需要支持全屏编辑,我考虑一下,尽快实现这个功能。
本文主要讲述如何实现在两个嵌套的Repeater之间插入另外的Control。即实现这样的嵌套:










这里的嵌套与一般的Repeater嵌套不同之处就是多了个控件Advancedpanel1。
对于一般的Repeater嵌套, 可以这样进行数据绑定, 代码如下:


<%# DataBinder.Eval(Container.DataItem,"Title") %>
<br>








嵌套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")。完整的代码如下:


<%# DataBinder.Eval(Container.DataItem,"Title") %>
<br>








BTW: 目前博客园编辑器的编辑区太小,文章长的时候,编辑起来很不方便,要经常上下移动,看来需要支持全屏编辑,我考虑一下,尽快实现这个功能。
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】凌霞软件回馈社区,博客园 & 1Panel & Halo 联合会员上线
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】博客园社区专享云产品让利特惠,阿里云新客6.5折上折
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 一个奇形怪状的面试题:Bean中的CHM要不要加volatile?
· [.NET]调用本地 Deepseek 模型
· 一个费力不讨好的项目,让我损失了近一半的绩效!
· .NET Core 托管堆内存泄露/CPU异常的常见思路
· PostgreSQL 和 SQL Server 在统计信息维护中的关键差异
· DeepSeek “源神”启动!「GitHub 热点速览」
· 我与微信审核的“相爱相杀”看个人小程序副业
· 微软正式发布.NET 10 Preview 1:开启下一代开发框架新篇章
· C# 集成 DeepSeek 模型实现 AI 私有化(本地部署与 API 调用教程)
· spring官宣接入deepseek,真的太香了~