博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
重写Repeater,使其支持空模版(列表为空时显示) 。改进
阅读量:7227 次
发布时间:2019-06-29

本文共 1432 字,大约阅读时间需要 4 分钟。

最近项目需要,在网上搜索Repeater为空时提示解决方法。有两种方法,一种是在FooterTemplate模板中放一个隐藏控件,用Repeater.Items.Count ==0 判断是否显示。比如 就是用的这种方法。
第二种方法相对复杂一点,用的是重写Repeater。具体方法见 。
 
不过这两篇文章的方法都有点小问题,显示在FooterTemplate模板中间或之后,有什么缺点这就不说了,这里说下解决方法。
直接上代码:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using System.Text;
using System.Web.UI;
namespace MyRepeater
{
    
public
class Repeater
: System.Web.UI.WebControls.Repeater
    {
        [PersistenceMode(PersistenceMode.InnerProperty), Browsable(
false), TemplateContainer(
typeof(TemplateControl))]
        
public ITemplate EmptyDataTemplate{get;set;}
        
protected
override
void OnDataBinding(EventArgs e)
        {
            
base.OnDataBinding(e);
            
if (EmptyDataTemplate
!=
null)
            {
                
if (
this.Items.Count
==
0)
                {
                    EmptyDataTemplate.InstantiateIn(
this);
                }
            }
        }
        
protected
override
void RenderChildren(HtmlTextWriter output)
        {
            
if (HasControls())
            {
                
for (
int i
=
0; i
< Controls.Count; i
++)
                {
                    
if (
this.FooterTemplate
!=
null
&&
this.Items.Count
==
0
&& EmptyDataTemplate
!=
null)
                    {
                        
if (i
== Controls.Count
-
2)
                        {
                            Controls[i
+
1].RenderControl(output);
                            
continue;
                        }
                        
if (i
== Controls.Count
-
1)
                        {
                            Controls[i
-
1].RenderControl(output);
                            
continue;
                        }
                    }
                    Controls[i].RenderControl(output);
                }
            }
        }
        
protected
override
void Render(HtmlTextWriter output)
        {
            RenderChildren(output);
        }
    }
}

效果测试

前台代码

 

后台代码

 

显示效果

 

生成代码

 

源码地址,包括测试代码

http://my-repeater.googlecode.com/svn

转载于:https://www.cnblogs.com/weapon/archive/2012/11/22/2782468.html

你可能感兴趣的文章
在 Delphi 下使用 DirectSound (17): 频率均衡效果器 IDirectSoundFXParamEq8
查看>>
文件操作命令一cp 2
查看>>
Multi-Mechanize工程目录结构说明
查看>>
halt
查看>>
标准ACL+扩展ACL+命名ACL
查看>>
Meteor应用的启动过程分析
查看>>
九曲黄河万里沙,浪淘风簸自天涯 — 正则表达式
查看>>
欲哭无泪,联想笔记本性价比
查看>>
很简单的在Ubuntu系统下安装字体和切换默认字体的方法
查看>>
我的友情链接
查看>>
dojo框架用hitch实现函数与上下文的绑定
查看>>
ubuntu编译安装vim7.4
查看>>
python之利用PIL库实现页面的图片验证码及缩略图
查看>>
IP-COM设置×××
查看>>
VPC配置案例
查看>>
十年IT运维谈(五):要专业化还是平台化?
查看>>
分享超级给力的一个外发光Shader
查看>>
oblog_4.6_SQL 语句
查看>>
通过Git WebHooks+脚本实现自动更新发布代码之shell脚本
查看>>
对象实例化、字符串的使用方法
查看>>