韩言福地

只有想不到的,没有办不到的。 - HY Auspicious Place

« 如何致富在Global中Application_Error事件处理错误信息 »

使Repeater在数据为空时显示提示信息

扩展Repeater——让Repeater具有EmptyDataTemplate模板功能!
Repeater控件是一个数据显示控件,该控件允许通过为列表中显示的每一项重复使用指定的模板来自定义布局。
相对于GridViews和DataList来说Repeater是一个相当轻巧、灵活的控件,能耗也比它们要小很多。美中不足的是功能略显单薄,尤其是用于绑定的数据源没有数据的情况下,往往要使用一个隐藏面版来显示“暂时没有任何数据”信息。太麻烦了。
于是自行扩展了Repeater控件的EmptyDataTemplate模板已达到同样的效果。
    /**//// <summary>
    /// 自定义Repeater 支持EmptyDataTemplate
    /// 作者:cantops
    /// </summary>
    public class Repeater :System.Web.UI.WebControls.Repeater
    {
        private ITemplate emptyDataTemplate;

        [PersistenceMode(PersistenceMode.InnerProperty), TemplateContainer(typeof(TemplateControl))]
        public ITemplate EmptyDataTemplate
        {
            get { return emptyDataTemplate; }
            set { emptyDataTemplate = value; }
        }
        protected override void OnDataBinding(EventArgs e)
        {

            base.OnDataBinding(e);
            if (emptyDataTemplate != null)
            {
                if (this.Items.Count == 0)
                {
                    EmptyDataTemplate.InstantiateIn(this);
                }
            }
        }

    }

发表评论:

◎欢迎参与讨论,请在这里发表您的看法、交流您的观点。

日历

最新评论及回复

最近发表

Copyright 2007-2010 www.yinrg.com(HY Auspicious Place) . 湘ICP备06007796号.