独上高楼网站
  •    你所在位置:首页 VS.netASP.net控件〉AspNetPager的使用
  • AspNetPager的使用
  • 作者:烂纸  文章来源:网上下载  发布日期:2007-05-25  浏览次数:1503
  • 打印这篇文章
  • 这个控件有人说在2.0下面不能使用,试了一下,看来说法有误哦!仍然可以使用,方法也一样。下面把方法说明一下。

    1.AspNetPager控件放入工具箱的方法是右键点击工具箱,选择添加项目,然后刘览相关dll文件。

    2.控件外观的设定

       <webdiyer:AspNetPager ID="AspNetPager1" runat="server" UrlPaging="true" PageSize="5" ShowCustomInfoSection="Left" NumericButtonTextFormatString="[{0}]" ShowBoxThreshold="5" AlwaysShow="true" OnPageChanged="AspNetPager1_PageChanged"  >

       webdiyer:AspNetPager>

       其实,一些属性我也不懂是什么,ShowCustomInfoSection大约是一个安放自定义文本的东东。PageSize设定分页显示的记录笔数。OnPageChanged事件调用后台的方法。

    3.设定总的记录笔数在Page_Load事件里面

       this.AspNetPager1.RecordCount = pager.GetAuthorsRowsCount("ahthors");

       这里计算记录总笔数的方法是:

        ///

        /// 通用方法用于计算记录笔数

        ///

        ///

        ///

        public int ExecuteCount(string mySql)

        {

            SqlCommand myCmd = new SqlCommand(mySql, myConn);

            myCmd.CommandText = mySql;

            try

            {

                myConn.Open();

                return (int)myCmd.ExecuteScalar();

            }

            catch (Exception ex)

            {

                return -99;

            }

            finally

            {

                myCmd.Dispose();

                myConn.Close();

            }

        }

     

        ///

        /// 得到当前记录的笔数

        ///

        ///

        ///

        public int GetAuthorsRowsCount(string tablename)

        {

            string sql = "select count(*) from authors";

            return this.ExecuteCount(sql);

        }

     

      3,将部分数据插入数据集,并绑定到DATAGRID中。

        通用方法:

        ///

        /// 得到数据集用于分页的方法

        ///

        /// 要执行的查询语句

        /// 从哪一笔数据开始插入数据

        /// 共插入多少笔数据

        /// 给插入数据集中的表命名

        /// 数据集

        public DataSet ExecuteSqlDsReapter(string mySql, int reapterstr1, int reapterstr2, string myTable)

        {

     

            SqlCommand myCmd = new SqlCommand(mySql, myConn);

            SqlDataAdapter myDa = new SqlDataAdapter(myCmd);

            DataSet dsReapter = new DataSet();

            try

            {

                myDa.Fill(dsReapter, reapterstr1, reapterstr2, myTable);

                return dsReapter;

            }

            catch (Exception ex)

            {

                return new DataSet();

            }

            finally

            {

                myDa.Dispose();

                myConn.Close();

            }

        }

     

        得到当前数据集

        ///

        /// 得到数据集

        ///

        /// 给填充到数据集的表命名

        /// 从哪一笔记录开始插入数据

        /// 共插入几笔数据

        ///

        public DataSet  GetAuthorsRows(string table,int repeater1,int repeaterstr2)

        {

            string sql = "select * from authors";

            return this.ExecuteSqlDsReapter(sql, repeater1, repeaterstr2, table);

        }

     

    绑定到DATAGRID ,这里只是举DATAGRID例,GRIDVIEW我没试过。

        ///

        /// 有两个任务:绑定数据集;显示记录信息

        ///

        public void DataBindChannel()

        {

            //绑定数据集

            DataSet list = new DataSet();

            int repeater1 = AspNetPager1.PageSize * (AspNetPager1.CurrentPageIndex - 1);

            int repeater2 = AspNetPager1.PageSize;

            list = pager.GetAuthorsRows("authours", repeater1, repeater2);

            dg.DataSource = list.Tables["authours"];

            dg.DataBind();

     

            //显示记录信息

            AspNetPager1.CustomInfoText = "记录总数:"" + AspNetPager1.RecordCount.ToString() + ";

            AspNetPager1.CustomInfoText += " 总页数:"" + AspNetPager1.PageCount.ToString() + ";

            AspNetPager1.CustomInfoText += " 当前页:"" + AspNetPager1.CurrentPageIndex.ToString() + ";

        }

         

         分页的方法

    ///

        /// 点分页按钮时调用的方法

        ///

        ///

        ///

     

        protected void AspNetPager1_PageChanged(object src, Wuqi.Webdiyer.PageChangedEventArgs e)

        {

            AspNetPager1.CurrentPageIndex = e.NewPageIndex;

            DataBindChannel();

        }

  • 打印这篇文章
  • 与本文主题相关的文章
  • 返回首页