zoukankan      html  css  js  c++  java
  • 重写Repeater,使其支持空模版(列表为空时显示)

    在WebForm项目中,列表显示我比较喜欢用Repeater,生成的代码比较干净。不过有一个问题就是当列表为空的时候,为了有较好的用户体验,我们喜欢在界面给出一行提示“查询结果为空”。这时候,.NET自带的Repeater就不能满足需求了,以前通常都是查询出来以后判断一下列表项,一般是datatable或list,如果条数为0,就给出提示,每个列表进行判断,重复代码太多。所以,这里我们想到重写一下Repeater,使其支持一个"空模板",直接代码:

    namespace szfsControl
    {
        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);
                    }
                }
            }
        }
    }

    界面上调用,首先添加:

    <%@ Register TagPrefix="Szfs" Namespace="szfsControl" Assembly="Szfs.Web" %>

    然后调用我们重写过的 Repeater
             <Szfs:Repeater runat="server" ID="Repeater1">
                <ItemTemplate>
                    数据绑定
                </ItemTemplate>
                <EmptyDataTemplate>
                    查询结果为空
                </EmptyDataTemplate>
             </Szfs:Repeater>
  • 相关阅读:
    javax.management.InstanceNotFoundException: org.springframework.boot:type=Admin,name=SpringApplicati
    了解燃尽图
    hibernate错误org.hibernate.AnnotationException: No identifier specified for entity:
    JGit的常用功能(提交、回滚,日志查询)
    CoverageBuilder
    IntelliJ IDEA生成jar包运行报Error:A JNI error has occurred,please check your installation and try again
    jacoco-实战篇-增量覆盖率
    代码覆盖率-JaCoCo
    Hard voting and Soft Voting
    AP聚类
  • 原文地址:https://www.cnblogs.com/kdalan/p/2569583.html
Copyright © 2011-2022 走看看