深入 DataList 分页技术之使用 PagedDataSource 篇

翻译|其它|编辑:郝浩|2007-06-06 13:30:22.000|阅读 2461 次

概述:

# 界面/图表报表/文档/IDE等千款热门软控件火热销售中 >>

    这次使用 PagedDataSource 类来实现 DataList 分页技术,因为这个类封装了 DataGird 的分页属性,我们来使用它来实现分页,我在这里做到了,第一页,上一页,下一页,最后一页,跳转到第几页这几个功能。这些功能应该都比较实用。希望对大家有所用,给大家看代码吧。

using System;

using System.Collections;

using System.ComponentModel;

using System.Data;

using System.Drawing;

using System.Web;

using System.Web.SessionState;

using System.Web.UI;

using System.Web.UI.WebControls;

using System.Web.UI.HtmlControls;

using System.Data.SqlClient;

namespace DataListTest1

{

       /// <summary>

       /// WebForm1 的摘要说明。

       /// </summary>

       public class WebForm1 : System.Web.UI.Page

       {

              protected System.Web.UI.WebControls.DataList DataList1;

              protected System.Web.UI.WebControls.HyperLink HlkFirst;

              protected System.Web.UI.WebControls.HyperLink HlkPrevious;

              protected System.Web.UI.WebControls.HyperLink HlkNext;

              protected System.Web.UI.WebControls.HyperLink HlkLast;

              protected System.Web.UI.WebControls.Label LblTotalRecord;

              protected System.Web.UI.WebControls.Label LblTotalPages;

              protected System.Web.UI.WebControls.Label LblCurrent;

              protected System.Web.UI.WebControls.LinkButton LinkButton1; 

              protected System.Web.UI.WebControls.TextBox TbPageIndex;

              private void Page_Load(object sender, System.EventArgs e)

              {

                     // 在此处放置用户代码以初始化页面

                     if(!this.IsPostBack)

                     {

                            this.bindtoGL();

                     }

              }

              //绑定数据到 DataList

              private void bindtoGL()

              {

            SqlConnection  con=new SqlConnection("server=.;database=Northwind;uid=sa;pwd=");

                     con.Open();

                     SqlDataAdapter sda=new SqlDataAdapter();

                     sda.SelectCommand=new SqlCommand("select * from Orders",con);

                     DataSet ds=new DataSet();

                     sda.Fill(ds,"orders");

                  PagedDataSource pds=new PagedDataSource();

                     pds.DataSource=ds.Tables["orders"].DefaultView;

                     pds.AllowPaging=true;

                     pds.PageSize=12;

                     int  CurrentPage;

                     if(Request.QueryString["pageIndex"]!=null)

                     {

                            CurrentPage=Convert.ToInt32(Request.QueryString["pageIndex"]);

                     }

                     else

                     {

                            CurrentPage=1;

                     }

                    

                     pds.CurrentPageIndex=CurrentPage-1;

//CurrentPageIndex 是从0页开始的所以要减1

                     this.LblCurrent.Text=CurrentPage.ToString();//显示是从第1页开始

                     //上一页导航

                     if(!pds.IsFirstPage)

                            this.HlkPrevious.NavigateUrl=Request.CurrentExecutionFilePath+"?pageIndex="+Convert.ToString(CurrentPage-1);

                     //下一页导航

                     if(!pds.IsLastPage)

                            this.HlkNext.NavigateUrl=Request.CurrentExecutionFilePath+"?pageIndex="+Convert.ToString(CurrentPage+1);

                  //第一页导航

              this.HlkFirst.NavigateUrl=Request.CurrentExecutionFilePath+"?pageIndex="+1;

                     //最后一页导航              this.HlkLast.NavigateUrl=Request.CurrentExecutionFilePath+"?pageIndex="+pds.PageCount;

                     this.DataList1.DataSource=pds;

                     this.DataList1.DataBind();

              }  

        //这个是最后一页的按钮点击事件

              private void LinkButton1_Click(object sender, System.EventArgs e)

              {

                     SqlConnection  con=new SqlConnection("server=.;database=Northwind;uid=sa;pwd=");

                     con.Open();

                     SqlDataAdapter sda=new SqlDataAdapter();

                     sda.SelectCommand=new SqlCommand("select * from Orders",con);

                     DataSet ds=new DataSet();

                     sda.Fill(ds,"orders");

                     PagedDataSource pds=new PagedDataSource();

                     pds.DataSource=ds.Tables["orders"].DefaultView;

                     pds.AllowPaging=true;

                     pds.PageSize=12;

       if(this.TbPageIndex.Text.ToString()==""||Convert.ToInt32(this.TbPageIndex.Text.Trim())>=pds.PageCount)

                     {

                            Response.Write("输入不能为空,或则数字不能超过总页数!");

                     }

                     else

                     {

       pds.CurrentPageIndex=Convert.ToInt32(this.TbPageIndex.Text.Trim())-1;

                     }

               this.LblCurrent.Text=this.TbPageIndex.Text;

                     this.DataList1.DataSource=pds;

                     this.DataList1.DataBind();

              }

       }

}


标签:

本站文章除注明转载外,均为本站原创或翻译。欢迎任何形式的转载,但请务必注明出处、不得修改原文相关链接,如果存在内容上的异议请邮件反馈至chenjj@evget.com


为你推荐

  • 推荐视频
  • 推荐活动
  • 推荐产品
  • 推荐文章
  • 慧都慧问
扫码咨询


添加微信 立即咨询

电话咨询

客服热线
023-68661681

TOP