[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: 目前博客园编辑器的编辑区太小,文章长的时候,编辑起来很不方便,要经常上下移动,看来需要支持全屏编辑,我考虑一下,尽快实现这个功能。
posted @ 2004-10-01 18:12 dudu 阅读(5633) 评论(15)  编辑 收藏 网摘

  回复  引用  查看    
#1楼 2004-10-01 19:01 | wayfarer      
这个问题看来是小问题,最后的解决方法也很简单。但却提供了一种思路,就是走迂回的路线。

如果无法获得控件本身的Item,则通过其Parent控件来获得,嗯,办法很好,值得借鉴。
  回复  引用  查看    
#2楼 2004-10-02 09:16 | hsb      
DataGrid的某列是下拉列表框列,我要取得同行中的其他列的控件
,我用了下面的语句:
DataGridItem dgi = (DataGridItem)(((DropDownList)sender).Parent.Parent);
DropDownList lst = (DropDownList)dgi.Cells[3].Controls[1];

另外,用ToString()得到对象的类型,会对写代码有提示作用。
  回复  引用  查看    
#3楼 2004-10-02 09:58 | Rover      
使用FindControl给嵌套的Repeater邦定数据
  回复  引用  查看    
#4楼 2004-10-02 15:07 | myrat      
我现在也越来越喜欢这种方法了,不喜欢使用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
  回复  引用    
#5楼 2004-11-02 14:30 | seaonce [未注册用户]
感谢楼主,这问题困扰我很久了,一直没找到答案,非常感谢。
不过我还是不大明白你写的代码是如何实现的。
一级如何结二级传递值?

希望指教

MSN:SEAONCE@HOTMAILL.COM
  回复  引用  查看    
#6楼 [楼主]2004-11-02 14:36 | dudu      
DataSource='<%# GetGlobalCategory(int.Parse(DataBinder.Eval(Container.DataItem, "CategoryID").ToString())) %>'>

  回复  引用    
#7楼 2005-06-09 16:21 | liujun [未注册用户]
protected LinkCategoryCollection GetGlobalCategory(int ParentID)
{
return Links.GetCategoriesByType(CategoryType.Global,ParentID,false);
}
中的LinkCategoryCollection是什么东东?
  回复  引用  查看    
#8楼 [楼主]2005-06-09 16:27 | dudu      
LinkCategoryCollection是一个存储分类数据的集合类。
  回复  引用    
#9楼 2005-06-09 16:44 | zjroland [未注册用户]
return Links.GetCategoriesByType(CategoryType.Global,ParentID,false);
我知道GetCategoriesByType(CategoryType.Global,ParentID,false)是函数方法,但是 Links是什么,是您程序中的类名吗?

  回复  引用  查看    
#10楼 [楼主]2005-06-09 16:49 | dudu      
这是举例, 你无需关心这个。
  回复  引用    
#11楼 2005-06-09 16:55 | zjroland [未注册用户]
找不到类型或命名空间名称“LinkCategoryCollection”(是否缺少 using 指令或程序集引用?)

  回复  引用    
#12楼 2005-08-13 16:56 | java、.Net技术网 [未注册用户]
强烈要求楼主贴出全部代码。我看的是懂非懂,本机测试又错误。
  回复  引用    
#13楼 2006-02-22 08:19 | Shen126 [未注册用户]
非常感谢!
看似简短的文字却解决了我的大疑惑。
  回复  引用    
#14楼 2006-12-18 10:25 | baxiqiuxing [未注册用户]
看了dudu提供的CNBlogsDottext10Beta2源码,在网站分类SiteCategory.ascx中,并未使用Advancedpanel1,
下面是二级分类的源码,也没使用Parent<asp:Repeater id="CategoryLevel2" runat="server" DataSource='<%# GetGlobalCategory(int.Parse(DataBinder.Eval(Container.DataItem, "CategoryID").ToString())) %>' Visible='<%# Dottext.Framework.Configuration.Config.Settings.CategoryDepth==2 %>'>
不知dudu的Advancedpanel1在哪出现了,
想问下dudu,ANW中的类都赶些什么
  回复  引用    
#15楼 2008-05-02 17:03 | s_3331 [未注册用户]
无法将类型为“System.Web.UI.WebControls.Repeater”的对象强制转换为类型“System.Web.UI.WebControls.RepeaterItem”。




标题  
姓名  
主页
Email (博主才能看到) 
验证码 *  看不清,换一张 [登录][注册]
内容(请不要发表任何与政治相关的内容)  
  登录  使用高级评论  新用户注册  返回页首  恢复上次提交      
该文被作者在 2004-10-02 19:26 编辑过
Google站内搜索

China-pub 计算机图书网上专卖店!6.5万品种 2-8折!
近千种 9-95 新二手计算图书火热销售中!
开发者征途系统新作:《设计模式——基于C#的工程化实现及扩展》

相关文章:

相关链接: