现在的位置: 首页 > 综合 > 正文

Visual Studio 2005 GridView分页方法

2013年04月22日 ⁄ 综合 ⁄ 共 7501字 ⁄ 字号 评论关闭

1、页面上

<asp:GridView runat="server" AutoGenerateColumns="False" AllowPaging="True" Width="100%"  AllowSorting="True"  OnPageIndexChanging="Case_GV_PageIndexChanging" DataKeyNames="ObjectID" DataSourceID="dataSource" OnDataBound="Case_GV_DataBound" OnSorting="Case_GV_Sorting">

</asp:GridView>

<asp:ObjectDataSource ID="dataSource" runat="server"></asp:ObjectDataSource>

2、页面后台代码,在Page_Load(…)时执行BindData()

       #region 绑定案件列表 BindData()

        /// <summary>

        /// 绑定案件列表

        /// </summary>

        private void BindData()

        {

            //初始化aspx上的数据源

            InitDataSource();

            dataSource.SelectParameters["opathStr"].DefaultValue = OPath;

            dataSource.SelectParameters["userID"].DefaultValue = CurrentUser.UniqueID;

            //US的个性化设置中获取PageSize

            Case_GV.PageSize = int.Parse(CurrentUser.Profile["PageSize"].Value.ToString());

           

        }

 

        /// <summary>

        /// 初始化aspx上的数据源

        /// </summary>

        private void InitDataSource()

        {

            Parameter opathPara = new Parameter("opathStr", TypeCode.String, OPath);

            Parameter userPara = new Parameter("userID", TypeCode.String, CurrentUser.UniqueID);

            dataSource.EnablePaging = true;

            dataSource.StartRowIndexParameterName = "startIndex";

            dataSource.MaximumRowsParameterName = "endIndex";

            //获取案件数目的方法

            dataSource.SelectCountMethod = "GetViewCaseCount";

            //获取案件对象的方法

            dataSource.SelectMethod = "GetViewCaseList";

            dataSource.SortParameterName = "sortExpression";

            dataSource.TypeName = "Com.iFlytek.PoliceOnline2.Business.DataObjectsManger.CaseManager";

 

            if (dataSource.SelectParameters["opathStr"] == null)

            {

                dataSource.SelectParameters.Add(opathPara);

            }

            if (dataSource.SelectParameters["userID"] == null)

            {

                dataSource.SelectParameters.Add(userPara);

            }

        }

        #endregion

3、业务层获取数据的方法  Namespace Com.iFlytek.PoliceOnline2.Business.DataObjectsManger.CaseManager. GetViewCaseList(……):List<Case>

 

Namespace Com.iFlytek.PoliceOnline2.Business.DataObjectsManger

Class CaseManager

{

        public static List<Case> GetViewCaseList(string userID, string opathStr, int startIndex, int endIndex, string sortExpression)

        {

            if (sortExpression == "")

            {

                sortExpression = "InsertTime Desc";

            }

            if ((opathStr == "")||(opathStr == null))

            {

                opathStr = "1=1";

            }

            ObjectSet objectSet = new ObjectSet();

            objectSet = NodObjectManager.GetNodObjectSet(string.Format("Case[{0} and {1}]", RightManager.GetViewRightString(userID), opathStr), startIndex, endIndex, sortExpression);

            List<Case> list = new List<Case>();

            foreach (DataObject dataObject in objectSet)

            {

                list.Add((Case)dataObject);

            }

        

            return list;

        }

}

 

4、底层获取数据的管理类

namespace Com.iFlytek.PoliceOnline2.Business

{

         /// <summary>

         /// NodObject管理类

         /// </summary>

    internal static class NodObjectManager

         {

 

                   /// <summary>

                   /// 获取单个DataObject

                   /// </summary>

                   /// <param ></param>

                   /// <returns></returns>

                   internal static DataObject GetNodObject(string opathStr)

                   {

                            DataObject obj = NodObject.CurrentSession.GetObject(opathStr);

 

                            return obj;

                   }

 

        /// <summary>

        /// 获取ObjectSet

        /// </summary>

        /// <param ></param>

        /// <param ></param>

        /// <returns></returns>

        internal static ObjectSet GetNodObjectSet(string opathStr, string sortExpression)

        {

            return GetNodObjectSet(opathStr, 0, -1, sortExpression);

        }

 

                   /// <summary>

                   /// 获取ObjectSet,实现分页功能

                   /// </summary>

                   /// <param >OPath语句</param>

                   /// <param >开始记录数</param>

                   /// <param >结束记录数</param>

                   /// <param >排序表达式:IdId DESC</param>

                   /// <returns></returns>

                   internal static ObjectSet GetNodObjectSet(string opathStr,int startIndex,int endIndex,string sortExpression)

                   {

                            string orderby = string.Empty;

                            if (!string.IsNullOrEmpty(sortExpression))

                            {

                                     if (!sortExpression.ToUpper().Contains("DESC"))

                                     {

                                               orderby = string.Format(" Orderby ({0} ASC)", sortExpression);

                                     }

                                     else

                                     {

                                               orderby = string.Format(" Orderby ({0})", sortExpression);

                                     }

                            }

 

                            ObjectReader objReader = NodObject.CurrentSession.GetObjectReader(string.Format("{0} {1}", opathStr, orderby));

 

                            while (0 < startIndex)

                            {

                                     if (!objReader.Read())

                                     {

                                               return new ObjectSet();

                                     }

                                     startIndex--;

                            }

 

                            ObjectSet objectSet = new ObjectSet();

                            if (endIndex < 0)

                                     endIndex = int.MaxValue;

 

                            while (endIndex > 0 && objReader.Read())

                            {

                                     objectSet.Add(objReader.Current);

                                     endIndex--;

                            }

                            objReader.Close();

                            return objectSet;

                   }

 

 

                   /// <summary>

                   /// 获取符合条件的对象数

                   /// </summary>

                   /// <param >OPath语句</param>

                   /// <returns></returns>

                   internal static int GetNodObjectCount(string opathStr)

                   {

                            return NodObject.CurrentSession.GetObjectCount(opathStr);

                   }

         }

}

 

说明:

A、在页面的代码中,标红的地方要注意,gridview设置了datasourceid,这就设置了gridview的数据源为id等于datasourceidobjectdatasource

 

B、具体数据层分页的代码在NodObjectManager. GetNodObjectSet(……): ObjectSet,通过ObjectReader来过滤不需要显示在页面的数据。

C、页面后台代码中InitDataSource()方法里面, dataSource.TypeName = "Com.iFlytek.PoliceOnline2.Business.DataObjectsManger.CaseManager";dataSource.SelectMethod = "GetViewCaseList";就指明了datasource获取数据时需要调用的类名和方法名,如果方法有重载那么怎么找到具体是那个方法呢?

 

            Parameter opathPara = new Parameter("opathStr", TypeCode.String, OPath);

            Parameter userPara = new Parameter("userID", TypeCode.String, CurrentUser.UniqueID);

dataSource.StartRowIndexParameterName = "startIndex";

            dataSource.MaximumRowsParameterName = "endIndex";

            dataSource.SortParameterName = "sortExpression";

            if (dataSource.SelectParameters["opathStr"] == null)

            {

                dataSource.SelectParameters.Add(opathPara);

            }

            if (dataSource.SelectParameters["userID"] == null)

            {

                dataSource.SelectParameters.Add(userPara);

            }

这部分代码就指明了这个方法所需要的参数,这个方法指明了有5个参数(opathStr,userID,startIndex,endIndex,sortExpression),asp.net回通过反射来寻找在Com.iFlytek.PoliceOnline2.Business.DataObjectsManger.CaseManager类中,有和以上五个参数同名的参数的方法GetViewCaseList,并自动调用他获取数据。

D、在InitDataSource()dataSource.SelectCountMethod = "GetViewCaseCount";为数据源指明了获取整个数据条数的方法。

抱歉!评论已关闭.