分页程序

本文介绍了一个基于ASP.NET的简单分页实现方案,通过SqlConnection连接数据库,并使用SqlCommand执行SQL语句来获取数据总数及每页的数据。此外,还实现了翻页按钮的功能,包括上一页、下一页等。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

ContractedBlock.gifExpandedBlockStart.gifCode
  1None.gifusing System;
  2None.gifusing System.Data;
  3None.gifusing System.Configuration;
  4None.gifusing System.Collections;
  5None.gifusing System.Web;
  6None.gifusing System.Web.Security;
  7None.gifusing System.Web.UI;
  8None.gifusing System.Web.UI.WebControls;
  9None.gifusing System.Web.UI.WebControls.WebParts;
 10None.gifusing System.Web.UI.HtmlControls;
 11None.gifusing System.Data;
 12None.gifusing System.Data.SqlClient;
 13None.gif
 14None.gifpublic partial class pager : System.Web.UI.Page
 15ExpandedBlockStart.gifContractedBlock.gifdot.gif{
 16InBlock.gif    string connstr = System.Configuration.ConfigurationManager.AppSettings["SQLCONNECTIONSTRING"];
 17InBlock.gif    //SqlConnection myconnection = new SqlConnection(connstr);
 18InBlock.gif    ArrayList Al_PageNum;
 19InBlock.gif    int PageSize;
 20InBlock.gif    int RecordCount;
 21InBlock.gif    int PageCount;
 22InBlock.gif    int CurrentPage;
 23InBlock.gif    protected void Page_Load(object sender, EventArgs e)
 24ExpandedSubBlockStart.gifContractedSubBlock.gif    dot.gif{
 25InBlock.gif        PageSize = 5;               //设定PageSize 
 26InBlock.gif        SqlConnection myconnection = new SqlConnection(connstr);
 27InBlock.gif        myconnection.Open();
 28InBlock.gif        if (!Page.IsPostBack)                  //第一次请求执行 
 29ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{
 30InBlock.gif            RecordCount = CalculateRecord(); //计算总共有多少记录/
 31InBlock.gif
 32InBlock.gif            PageCount = RecordCount / PageSize; //计算总共有多少页
 33InBlock.gif            if (RecordCount % PageSize > 0)     //取整 
 34InBlock.gif                PageCount = PageCount + 1;
 35InBlock.gif            lblPageCount.Text = PageCount.ToString();
 36InBlock.gif            lblRecordCount.Text = RecordCount.ToString();
 37InBlock.gif            ViewState["PageCount"= PageCount;
 38InBlock.gif            CurrentPage = 0;
 39InBlock.gif            ViewState["PageIndex"= 0;
 40InBlock.gif
 41InBlock.gif            Al_PageNum = new ArrayList();//绑定DROPDOWNLIST
 42InBlock.gif            for (int i = 1; i <= PageCount; i++)   //从1开始循环,为了不出现0页码
 43InBlock.gif                Al_PageNum.Add(i.ToString());
 44InBlock.gif            Ddl_PageNumber.DataSource = Al_PageNum;
 45InBlock.gif            Ddl_PageNumber.DataBind();
 46InBlock.gif            ListBind();                         //绑定 
 47ExpandedSubBlockEnd.gif        }

 48InBlock.gif        myconnection.Close();
 49InBlock.gif
 50InBlock.gif
 51ExpandedSubBlockEnd.gif    }

 52InBlock.gif    public int CalculateRecord()                //计算总共有多少条记录
 53ExpandedSubBlockStart.gifContractedSubBlock.gif    dot.gif{
 54InBlock.gif        SqlConnection myconnection = new SqlConnection(connstr);
 55InBlock.gif        int intCount;
 56InBlock.gif        string strCount = "select count(*) as co from News";
 57InBlock.gif        SqlCommand MyComm = new SqlCommand(strCount, myconnection);
 58InBlock.gif        myconnection.Open();
 59InBlock.gif        SqlDataReader dr = MyComm.ExecuteReader();
 60InBlock.gif        if (dr.Read())
 61ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{
 62InBlock.gif            intCount = Int32.Parse(dr["co"].ToString());
 63InBlock.gif           
 64ExpandedSubBlockEnd.gif        }

 65InBlock.gif        else
 66ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{
 67InBlock.gif            intCount = 0;
 68ExpandedSubBlockEnd.gif        }

 69InBlock.gif        dr.Close();
 70InBlock.gif        myconnection.Close();
 71InBlock.gif        return intCount;
 72ExpandedSubBlockEnd.gif    }

 73InBlock.gif
 74InBlock.gif
 75InBlock.gif    ICollection CreateSource()
 76ExpandedSubBlockStart.gifContractedSubBlock.gif    dot.gif{
 77InBlock.gif        SqlConnection myconnection = new SqlConnection(connstr);
 78InBlock.gif        int StartIndex;                               //设定导入的起终地址 
 79InBlock.gif        StartIndex = CurrentPage * PageSize;            //计算记录数的起始点
 80InBlock.gif        string strSel = "select title,author,updatatime from News order by ID desc ";
 81InBlock.gif        DataSet ds = new DataSet();
 82InBlock.gif        SqlDataAdapter MyAdapter = new SqlDataAdapter(strSel, myconnection);
 83InBlock.gif        MyAdapter.Fill(ds, StartIndex, PageSize, "News");
 84InBlock.gif        return ds.Tables["News"].DefaultView;
 85ExpandedSubBlockEnd.gif    }

 86InBlock.gif
 87InBlock.gif    public void ListBind()
 88ExpandedSubBlockStart.gifContractedSubBlock.gif    dot.gif{
 89InBlock.gif        MyList.DataSource = CreateSource();
 90InBlock.gif        MyList.DataBind();
 91InBlock.gif        lbnNextPage.Enabled = true;
 92InBlock.gif        lbnPrevPage.Enabled = true;
 93InBlock.gif        BtnFirst.Enabled = true;
 94InBlock.gif        BtnLast.Enabled = true;
 95InBlock.gif        if (PageCount == 0)
 96ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{
 97InBlock.gif            lblCurrentPage.Text = "0";
 98InBlock.gif            lbnNextPage.Enabled = false;
 99InBlock.gif            lbnPrevPage.Enabled = false;
100InBlock.gif            BtnFirst.Enabled = false;
101InBlock.gif            BtnLast.Enabled = false;
102ExpandedSubBlockEnd.gif        }

103InBlock.gif        else
104ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{
105InBlock.gif            if (CurrentPage == (PageCount - 1)) lbnNextPage.Enabled = false;
106InBlock.gif            if (CurrentPage == 0) lbnPrevPage.Enabled = false;
107InBlock.gif            lblCurrentPage.Text = (CurrentPage + 1).ToString();
108ExpandedSubBlockEnd.gif        }

109InBlock.gif        Ddl_PageNumber.Text = lblCurrentPage.Text;
110ExpandedSubBlockEnd.gif    }

111InBlock.gif
112InBlock.gif    public void Page_OnClick(Object sender, CommandEventArgs e)
113ExpandedSubBlockStart.gifContractedSubBlock.gif    dot.gif{
114InBlock.gif        CurrentPage = (int)ViewState["PageIndex"];
115InBlock.gif        PageCount = (int)ViewState["PageCount"];
116InBlock.gif        string cmd = e.CommandName;                 //判断cmd,以判定翻页方向 
117InBlock.gif
118InBlock.gif        switch (cmd)
119ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{
120InBlock.gif            case "next":
121InBlock.gif                if (CurrentPage < (PageCount - 1)) CurrentPage++;
122InBlock.gif                break;
123InBlock.gif            case "prev":
124InBlock.gif                if (CurrentPage > 0) CurrentPage--;
125InBlock.gif                break;
126InBlock.gif            case "Last":
127InBlock.gif                CurrentPage = (PageCount - 1);
128InBlock.gif                break;
129InBlock.gif            default:
130InBlock.gif                CurrentPage = 0;
131InBlock.gif                break;
132ExpandedSubBlockEnd.gif        }

133InBlock.gif
134InBlock.gif        ViewState["PageIndex"= CurrentPage;
135InBlock.gif        ListBind();
136ExpandedSubBlockEnd.gif    }

137InBlock.gif    public void PageNum_SelectIndexChanged(object sender, System.EventArgs e)
138ExpandedSubBlockStart.gifContractedSubBlock.gif    dot.gif{
139InBlock.gif        ViewState["PageIndex"= int.Parse(Ddl_PageNumber.SelectedItem.Value) - 1;//保持不出现0页码
140InBlock.gif        PageSize = 5;
141InBlock.gif        CurrentPage = (int)ViewState["PageIndex"];
142InBlock.gif        PageCount = (int)ViewState["PageCount"];
143InBlock.gif        ListBind();
144InBlock.gif        //MyList.DataSource = CreateSource(); 
145InBlock.gif        //MyList.DataBind(); 
146ExpandedSubBlockEnd.gif    }

147InBlock.gif
148InBlock.gif    override protected void OnInit(EventArgs e)
149ExpandedSubBlockStart.gifContractedSubBlock.gif    dot.gif{
150InBlock.gif        InitializeComponent();
151InBlock.gif        base.OnInit(e);
152ExpandedSubBlockEnd.gif    }

153InBlock.gif
154InBlock.gif    private void InitializeComponent()
155ExpandedSubBlockStart.gifContractedSubBlock.gif    dot.gif{
156InBlock.gif        this.Load += new System.EventHandler(this.Page_Load);
157InBlock.gif        this.Ddl_PageNumber.SelectedIndexChanged += new System.EventHandler(this.PageNum_SelectIndexChanged);
158ExpandedSubBlockEnd.gif    }

159InBlock.gif
160InBlock.gif
161InBlock.gif   
162ExpandedBlockEnd.gif}

163None.gif
164None.gif

转载于:https://www.cnblogs.com/shineqiujuan/archive/2008/08/31/1280659.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值