一聚教程网:一个值得你收藏的教程网站

热门教程

asp.net Repeater排序用的控件应用与详解

时间:2022-06-25 08:31:57 编辑:袖梨 来源:一聚教程网

asp教程.net repeater排序用的控件应用与详解

.aspx页代码

如果使用网站形式,那么将.cs文件放在app_code下,如果是webapplication则随便扔在那个目录下都可以

/----------------导入控件(网站方式--动态编译)--------------------/

<%@ register  namespace="f.studio.webcontrols" tagprefix="fs" %>

:也可以在web.config中做个全局配置,这样就不用每个页面引用一次了

/----------------repeater 控件部分--------------------------/

   


       
           
           
           
       
       
       
       

           
           
           
       
       
       
   

                id
               

               获得时间  

                最后联系时间 

                <%#eval("id") %>

                <%#eval("lastgettime") %>

               <%#eval("lastcontcttime") %>

/----------------------控件配置部分--------------------------/

   
   
   
   
   

控件属性与方法:

1.field-排序字段(可能为空)

2.isasc-排序方向

3.tostring()方法返回排序tsql语句(如:client.[clientid] asc),当field为空时,返回defaultorderbysql指定的语句

排序状态改变时会触发,onchange事件指定的处理函数,通常的处理是进行一次数据绑定, 参考下面代码:

        ///


        /// 排序后重新绑订数据
        ///

        protected void binddataafterorderby(orderbystatemanager orderby)
        {
            try
            {
                binddata();
            }
            catch (exception ex)
            {
                jscript.alert(ex.message);
            }
       }

 

orderbystatemanger.cs文件

using system;
using system.collections.generic;
using system.linq;
using system.web;
using system.runtime.serialization;
using system.web.ui.webcontrols;
using system.web.ui;
using system.componentmodel;
using system.collections;
using system.drawing.design;
using system.security.permissions;

namespace f.studio.webcontrols
{

    ///


    /// 单列排序结构类
    ///

    ///
    [serializable]
    [
        aspnethostingpermission(securityaction.demand,
            level = aspnethostingpermissionlevel.minimal),
        aspnethostingpermission(securityaction.inheritancedemand,
            level = aspnethostingpermissionlevel.minimal),
        defaultproperty("sortcolumns"),
        parsechildren(true, "sortcolumns"),
        toolboxdata("<{0}:orderbystatemanager runat="server"> ")

     ]
    public class orderbystatemanager : control
    {

        public event orderbystatechanged changed;

        public orderbystatemanager(string field, bool isasc)
        {
            field = field;
            isasc = isasc;
        }
        public orderbystatemanager()
        {

        }
        #region  集合属性
        private list _sortcolumns;
        [
         category("behavior"),
         description("排序字段与对应控件映射集合"),
         designerserializationvisibility(
             designerserializationvisibility.content),
         editor(typeof(ilist), typeof(uitypeeditor)),
         persistencemode(persistencemode.innerdefaultproperty)
        ]
        public list sortcolumns
        {
            get
            {
                if (_sortcolumns == null)
                {
                    _sortcolumns = new list();
                }
                return _sortcolumns;
            }
        }
        #endregion
        protected override void onload(eventargs e)
        {
            base.onload(e);

            sortcolumns.foreach(column =>
            {
                var ctr = page.findcontrol(column.linkbuttonid) as linkbutton;
                if (ctr != null)
                {
                    addmap(column.field, ctr);
                }
            });


        }


        ///


        /// sender 需要是 linkbutton类型
        ///

        ///
        public void setorderby(object sender)
        {

            var item = fieldtolnkmap.firstordefault(ent => ent.value.equals(sender));

            if (item.value == null) throw new exception("集合中没有指定触发排序事件linkbutton项目!");

            //顺序:上、下、取消
            if (string.isnullorempty(field))
            {
                isasc = true;
                field = item.key;
            }
            else
            {
                if (string.compare(field, item.key, true) == 0)//点同一个
                {
                    if (isasc) isasc = false;
                    else field = string.empty;

                }
                else
                {
                    field = item.key;
                    isasc = true;
                }
            }

            //只支持单个字段排序,清除全部控件的标志
            foreach (var kv in fieldtolnkmap)
            {
                kv.value.text = kv.value.text.trimend("↑,↓".tochararray());
            }

            if (!string.isnullorempty(field))
            {
                //触发控件加标志
                item.value.text += (isasc ? "↑" : "↓");

            }


            if (changed != null)
            {
                changed(this);
            }

        }

        #region 映射集合
        public void addmap(string field, linkbutton lnk)
        {
            lnk.click += orderbylnkclick;

            fieldtolnkmap.add(field, lnk);
        }

        private void orderbylnkclick(object sender, eventargs e)
        {
            setorderby(sender);
        }

        private dictionary _fieldtolnkmap;
        protected dictionary fieldtolnkmap
        {
            get
            {
                if (_fieldtolnkmap == null)
                {
                    _fieldtolnkmap = new dictionary();
                }
                return _fieldtolnkmap;
            }
        }
        #endregion


        #region 序列化字段
        const string c_field = @"obsm_field";
        public string field {
            get
            {
                  return viewstate[c_field] as string;
             }
            set
            {
                viewstate[c_field] = value;
            }
        }
        const string c_isasc = @"obsm_isasc";
        public bool isasc
        {
            get
            {
                if (viewstate[c_isasc] == null)
                {

                    viewstate[c_isasc] = true;
                }

                return (bool)viewstate[c_isasc];
            }
            set
            {
                viewstate[c_isasc] = value;
            }

        }

        [
         category("behavior"),
         defaultvalue(""),
         description("默认排序"),
        ]
        private string _defaultorderbysql = string.empty;
         public string defaultorderbysql {
             get
             {
                 return _defaultorderbysql;
             }
             set
             {
                 _defaultorderbysql = value;
             }
         }
        public override string tostring()
        {
            if (string.isnullorempty(field)) return defaultorderbysql;

            return string.format(" {0} {1} ", field, isasc ? "asc" : "desc");
        }

 

        #endregion

 
    }
    public delegate void orderbystatechanged(orderbystatemanager orderby);

    [serializable]
    [typeconverter(typeof(expandableobjectconverter))]
    public class sortcolumn
    {
        public sortcolumn()
        {

        }

        [
            category("behavior"),
            defaultvalue(""),
            description("排序字段"),
            notifyparentproperty(true),
        ]
        public string field { get; set; }

        [
         category("behavior"),
         defaultvalue(""),
         description("linkbutton控件id"),
         notifyparentproperty(true),
        ]
        public string linkbuttonid { get; set; }
    }
}

热门栏目