ASP.NET-FineUI开发实践-3

1.参照模拟数据库分页通过缓存重写内存分页,优化页面响应速度

Grid的响应速度是硬伤,我写了个通用方法把所有数据放在缓存中模拟数据库分页,比自带的缓存分页快很多,这里贴上实体类的通用方法,DataTable的就不贴了可以参照官方的自己写。记住要加上grid_PageIndexChange事件和grid_Sort事件,这里就不写了

  

        /// <summary>

        /// 获取数据源

        /// </summary>

        /// <returns></returns>

        private void GetData()

        {

            List<MsUser> lstUser = GetData();//数据库返回数据实体

            ViewState["lstUserData"] = JsonConvert.SerializeObject(lstUser);

        }



        /// <summary>

        /// 数据源绑定

        /// </summary>

        private void BindGridView()

        {

            string slstUserData = ViewState["lstUserData"].ToString();

            BindGridView<MsUser>(slstUserData, ref grid);

        }







         /// <summary>

        /// 绑定数据源

        /// </summary>

        /// <typeparam name="T">范数据</typeparam>

        /// <param name="vDataNew">数据Json</param>

        /// <param name="g">grid</param>

        public static void BindGridView<T>(string vDataNew, ref Grid g)

        {



            List<T> DataNew = (List<T>)JsonConvert.DeserializeObject(vDataNew, typeof(List<T>));

            g.RecordCount = DataNew.Count;

            int pageSize = g.PageSize;//获取页显示数

            int pageIndex = g.PageIndex;//获取当前页数

            int pi = Convert.ToInt32(Math.Ceiling(Convert.ToDouble(DataNew.Count) / Convert.ToDouble(g.PageSize)));

            pageIndex = pageIndex + 1 > pi ? pi - 1 : pageIndex;//重新绑定时(如查询)要判断页数

            if (pageIndex < 0)

            {

                pageIndex = 0;

            }

            try

            {

                Reverser<T> reverser = null;

                string SortField = g.SortField;//获取排序字段

                if (SortField == "")

                {

                    SortField = g.Columns[1].SortField;//默认第二列为排序列

                }

                reverser = new Reverser<T>(DataNew[0].GetType(), SortField, g.SortDirection == "ASC" ? ReverserInfo.Direction.ASC : ReverserInfo.Direction.DESC);//排序方法

                DataNew.Sort(reverser);

                if (pageSize <= DataNew.Count)

                {

                    int pz = pageSize < DataNew.Count - pageIndex * pageSize ? pageSize : DataNew.Count - pageIndex * pageSize;

                    if (pageSize == DataNew.Count)

                    {

                        pz = pageSize;

                    }

                    DataNew = DataNew.GetRange((pageIndex) * pageSize, pz);//截取数据

                }



                g.DataSource = DataNew;//绑定

                g.DataBind();



            }

            catch (Exception ex)

            {

                //Response.Write(ex.Message);

                g.DataSource = null;

                g.DataBind();

            }

            g.PageIndex = g.PageIndex + 1 > g.PageCount ? g.PageCount - 1 : g.PageIndex;

            if (g.PageIndex < 0)

            {

                g.PageIndex = 0;

            }

        }







     /*从网上下的实体类排序类*/

    /// <summary>

    /// 继承IComparer<T>接口,实现同一自定义类型 对象比较

    /// </summary>

    /// <typeparam name="T">T为泛用类型</typeparam>

    public class Reverser<T> : IComparer<T>

    {

        private Type type = null;

        private ReverserInfo info;



        /**/

        /// <summary>

        /// 构造函数

        /// </summary>

        /// <param name="type">进行比较的类类型</param>

        /// <param name="name">进行比较对象的属性名称</param>

        /// <param name="direction">比较方向(升序/降序)</param>

        public Reverser(Type type, string name, ReverserInfo.Direction direction)

        {

            this.type = type;

            this.info.name = name;

            if (direction != ReverserInfo.Direction.ASC)

                this.info.direction = direction;

        }





        /**/

        /// <summary>

        /// 构造函数

        /// </summary>

        /// <param name="className">进行比较的类名称</param>

        /// <param name="name">进行比较对象的属性名称</param>

        /// <param name="direction">比较方向(升序/降序)</param>

        public Reverser(string className, string name, ReverserInfo.Direction direction)

        {

            try

            {

                this.type = Type.GetType(className, true);

                this.info.name = name;

                this.info.direction = direction;

            }

            catch (Exception e)

            {

                throw new Exception(e.Message);

            }

        }



        /**/

        /// <summary>

        /// 构造函数

        /// </summary>

        /// <param name="t">进行比较的类型的实例</param>

        /// <param name="name">进行比较对象的属性名称</param>

        /// <param name="direction">比较方向(升序/降序)</param>

        public Reverser(T t, string name, ReverserInfo.Direction direction)

        {

            this.type = t.GetType();

            this.info.name = name;

            this.info.direction = direction;

        }



        //必须!实现IComparer<T>的比较方法。

        int IComparer<T>.Compare(T t1, T t2)

        {

            object x = this.type.InvokeMember(this.info.name, BindingFlags.Public | BindingFlags.Instance | BindingFlags.GetProperty, null, t1, null);

            object y = this.type.InvokeMember(this.info.name, BindingFlags.Public | BindingFlags.Instance | BindingFlags.GetProperty, null, t2, null);

            if (this.info.direction != ReverserInfo.Direction.ASC)

                Swap(ref x, ref y);

            return (new CaseInsensitiveComparer()).Compare(x, y);

        }



        //交换操作数

        private void Swap(ref object x, ref object y)

        {

            object temp = null;

            temp = x;

            x = y;

            y = temp;

        }

    }



    /**/

    /// <summary>

    /// 对象比较时使用的信息类

    /// </summary>

    public struct ReverserInfo

    {

        /**/

        /// <summary>

        /// 比较的方向,如下:

        /// ASC:升序

        /// DESC:降序

        /// </summary>

        public enum Direction

        {

            ASC = 0,

            DESC,

        };



        public enum Target

        {

            CUSTOMER = 0,

            FORM,

            FIELD,

            SERVER,

        };



        public string name;

        public Direction direction;

        public Target target;

    }

  

2.得到指定行指定列的值后台,rows的values是行的值,后台可以看到,通过列号找到数组的值,这个方法不是很保险但是聊胜于无

01.grid.Rows[rowindex].Values[grid.FindColumn(Columnid).ColumnIndex].ToString();  

  

3.按钮至少选择一项的通用方法,OnClientClick+=累加。

        /// <summary>

        /// 至少选择一项

        /// </summary>

        /// <param name="g"></param>

        /// <param name="ctrls"></param>

        private void NoSelectionNull(Grid g, params FineUI.Button[] ctrls)

        {

            foreach (FineUI.Button ctrl in ctrls)

            {

                ctrl.OnClientClick += g.GetNoSelectionAlertReference("至少选择一项!");

            }

        }

        

        protected void Page_Load(object sender, EventArgs e)

        {

            if (!IsPostBack)

            {

              NoSelectionNull(Grid1, Button4, Button2, Button3, Button6);

            }

        }

  

你可能感兴趣的:(asp.net)