嵌套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 on 2004-10-01 18:12 dudu 阅读(2203) 评论(12)   编辑  收藏 收藏至365Key 所属分类: CN.Text开发笔记

Feedback

#  re: [CN.Text开发笔记]嵌套Repeater的问题 2004-10-01 19:01 wayfarer
这个问题看来是小问题,最后的解决方法也很简单。但却提供了一种思路,就是走迂回的路线。

如果无法获得控件本身的Item,则通过其Parent控件来获得,嗯,办法很好,值得借鉴。
  

#  re: [CN.Text开发笔记]嵌套Repeater的问题 2004-10-02 09:16 hsb
DataGrid的某列是下拉列表框列,我要取得同行中的其他列的控件
,我用了下面的语句:
DataGridItem dgi = (DataGridItem)(((DropDownList)sender).Parent.Parent);
DropDownList lst = (DropDownList)dgi.Cells[3].Controls[1];

另外,用ToString()得到对象的类型,会对写代码有提示作用。
  

#  re: [CN.Text开发笔记]嵌套Repeater的问题 2004-10-02 09:58 Rover
使用FindControl给嵌套的Repeater邦定数据
  

#  re: [CN.Text开发笔记]嵌套Repeater的问题 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
  

#  re: [CN.Text开发笔记]嵌套Repeater的问题 2004-11-02 14:30 seaonce
感谢楼主,这问题困扰我很久了,一直没找到答案,非常感谢。
不过我还是不大明白你写的代码是如何实现的。
一级如何结二级传递值?

希望指教

MSN:SEAONCE@HOTMAILL.COM
  

#  re: [CN.Text开发笔记]嵌套Repeater的问题 2004-11-02 14:36 dudu
DataSource='<%# GetGlobalCategory(int.Parse(DataBinder.Eval(Container.DataItem, "CategoryID").ToString())) %>'>

  

#  re: [CN.Text开发笔记]嵌套Repeater的问题 2005-06-09 16:21 liujun
protected LinkCategoryCollection GetGlobalCategory(int ParentID)
{
return Links.GetCategoriesByType(CategoryType.Global,ParentID,false);
}
中的LinkCategoryCollection是什么东东?
  

#  re: [CN.Text开发笔记]嵌套Repeater的问题 2005-06-09 16:27 dudu
LinkCategoryCollection是一个存储分类数据的集合类。
  

#  re: [CN.Text开发笔记]嵌套Repeater的问题 2005-06-09 16:44 zjroland
return Links.GetCategoriesByType(CategoryType.Global,ParentID,false);
我知道GetCategoriesByType(CategoryType.Global,ParentID,false)是函数方法,但是 Links是什么,是您程序中的类名吗?

  

#  re: [CN.Text开发笔记]嵌套Repeater的问题 2005-06-09 16:49 dudu
这是举例, 你无需关心这个。
  

#  re: [CN.Text开发笔记]嵌套Repeater的问题 2005-06-09 16:55 zjroland
找不到类型或命名空间名称“LinkCategoryCollection”(是否缺少 using 指令或程序集引用?)

  

#  re: [CN.Text开发笔记]嵌套Repeater的问题 2005-08-13 16:56 java、.Net技术网
强烈要求楼主贴出全部代码。我看的是懂非懂,本机测试又错误。
#  re: [CN.Text开发笔记]嵌套Repeater的问题 2004-10-01 19:01 wayfarer
这个问题看来是小问题,最后的解决方法也很简单。但却提供了一种思路,就是走迂回的路线。

如果无法获得控件本身的Item,则通过其Parent控件来获得,嗯,办法很好,值得借鉴。
  

#  re: [CN.Text开发笔记]嵌套Repeater的问题 2004-10-02 09:16 hsb
DataGrid的某列是下拉列表框列,我要取得同行中的其他列的控件
,我用了下面的语句:
DataGridItem dgi = (DataGridItem)(((DropDownList)sender).Parent.Parent);
DropDownList lst = (DropDownList)dgi.Cells[3].Controls[1];

另外,用ToString()得到对象的类型,会对写代码有提示作用。
  

#  re: [CN.Text开发笔记]嵌套Repeater的问题 2004-10-02 09:58 Rover
使用FindControl给嵌套的Repeater邦定数据
  

#  re: [CN.Text开发笔记]嵌套Repeater的问题 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
  

#  re: [CN.Text开发笔记]嵌套Repeater的问题 2004-11-02 14:30 seaonce
感谢楼主,这问题困扰我很久了,一直没找到答案,非常感谢。
不过我还是不大明白你写的代码是如何实现的。
一级如何结二级传递值?

希望指教

MSN:SEAONCE@HOTMAILL.COM
  

#  re: [CN.Text开发笔记]嵌套Repeater的问题 2004-11-02 14:36 dudu
DataSource='<%# GetGlobalCategory(int.Parse(DataBinder.Eval(Container.DataItem, "CategoryID").ToString())) %>'>

  

#  re: [CN.Text开发笔记]嵌套Repeater的问题 2005-06-09 16:21 liujun
protected LinkCategoryCollection GetGlobalCategory(int ParentID)
{
return Links.GetCategoriesByType(CategoryType.Global,ParentID,false);
}
中的LinkCategoryCollection是什么东东?
  

#  re: [CN.Text开发笔记]嵌套Repeater的问题 2005-06-09 16:27 dudu
LinkCategoryCollection是一个存储分类数据的集合类。
  

#  re: [CN.Text开发笔记]嵌套Repeater的问题 2005-06-09 16:44 zjroland
return Links.GetCategoriesByType(CategoryType.Global,ParentID,false);
我知道GetCategoriesByType(CategoryType.Global,ParentID,false)是函数方法,但是 Links是什么,是您程序中的类名吗?

  

#  re: [CN.Text开发笔记]嵌套Repeater的问题 2005-06-09 16:49 dudu
这是举例, 你无需关心这个。
  

#  re: [CN.Text开发笔记]嵌套Repeater的问题 2005-06-09 16:55 zjroland
找不到类型或命名空间名称“LinkCategoryCollection”(是否缺少 using 指令或程序集引用?)

  

#  re: [CN.Text开发笔记]嵌套Repeater的问题 2005-08-13 16:56 java、.Net技术网
强烈要求楼主贴出全部代码。我看的是懂非懂,本机测试又错误。
#  re: [CN.Text开发笔记]嵌套Repeater的问题 2004-10-01 19:01 wayfarer
这个问题看来是小问题,最后的解决方法也很简单。但却提供了一种思路,就是走迂回的路线。

如果无法获得控件本身的Item,则通过其Parent控件来获得,嗯,办法很好,值得借鉴。
  

#  re: [CN.Text开发笔记]嵌套Repeater的问题 2004-10-02 09:16 hsb
DataGrid的某列是下拉列表框列,我要取得同行中的其他列的控件
,我用了下面的语句:
DataGridItem dgi = (DataGridItem)(((DropDownList)sender).Parent.Parent);
DropDownList lst = (DropDownList)dgi.Cells[3].Controls[1];

另外,用ToString()得到对象的类型,会对写代码有提示作用。
  

#  re: [CN.Text开发笔记]嵌套Repeater的问题 2004-10-02 09:58 Rover
使用FindControl给嵌套的Repeater邦定数据
  

#  re: [CN.Text开发笔记]嵌套Repeater的问题 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
  

#  re: [CN.Text开发笔记]嵌套Repeater的问题 2004-11-02 14:30 seaonce
感谢楼主,这问题困扰我很久了,一直没找到答案,非常感谢。
不过我还是不大明白你写的代码是如何实现的。
一级如何结二级传递值?

希望指教

MSN:SEAONCE@HOTMAILL.COM
  

#  re: [CN.Text开发笔记]嵌套Repeater的问题 2004-11-02 14:36 dudu
DataSource='<%# GetGlobalCategory(int.Parse(DataBinder.Eval(Container.DataItem, "CategoryID").ToString())) %>'>

  

#  re: [CN.Text开发笔记]嵌套Repeater的问题 2005-06-09 16:21 liujun
protected LinkCategoryCollection GetGlobalCategory(int ParentID)
{
return Links.GetCategoriesByType(CategoryType.Global,ParentID,false);
}
中的LinkCategoryCollection是什么东东?
  

#  re: [CN.Text开发笔记]嵌套Repeater的问题 2005-06-09 16:27 dudu
LinkCategoryCollection是一个存储分类数据的集合类。
  

#  re: [CN.Text开发笔记]嵌套Repeater的问题 2005-06-09 16:44 zjroland
return Links.GetCategoriesByType(CategoryType.Global,ParentID,false);
我知道GetCategoriesByType(CategoryType.Global,ParentID,false)是函数方法,但是 Links是什么,是您程序中的类名吗?

  

#  re: [CN.Text开发笔记]嵌套Repeater的问题 2005-06-09 16:49 dudu
这是举例, 你无需关心这个。
  

#  re: [CN.Text开发笔记]嵌套Repeater的问题 2005-06-09 16:55 zjroland
找不到类型或命名空间名称“LinkCategoryCollection”(是否缺少 using 指令或程序集引用?)

  

#  re: [CN.Text开发笔记]嵌套Repeater的问题 2005-08-13 16:56 java、.Net技术网
强烈要求楼主贴出全部代码。我看的是懂非懂,本机测试又错误。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值