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>
  • 相关阅读:
    1.开始学习ASP.NET MVC
    2.ASP.NET MVC 中使用Crystal Report水晶报表
    jQuery:SP.NET Autocomplete Textbox Using jQuery, JSON and AJAX
    JqGrid: paging int asp.net
    JqGrid: Add,Edit,Del in asp.net
    Python 3.4:Chromedrive,IEDriverServer,geckoDriver
    Python3.4:splinter or traceback
    postgresql-10.1-3-windows-x64 安装之后,起动pgAdmin 4问题(win10)
    Csharp:Paging Sorting Searching In ASP.NET MVC 5
    javascript:jQuery tablesorter 2.0
  • 原文地址:https://www.cnblogs.com/kdalan/p/2569583.html
Copyright © 2011-2022 走看看