小成博客

2010年到了...
  首页  :: 新随笔  :: 联系 :: 订阅 订阅  :: 管理

数字分页

Posted on 2007-07-04 22:35  成斌  阅读(833)  评论(0)    收藏  举报
using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Text;
using apk.DatabaseEx;
using System.Data.SqlClient;

public partial class MyCeShiPage : System.Web.UI.Page
{
    
protected void Page_Load(object sender, EventArgs e)
    
{
        
if (!IsPostBack)
        
{

            
if (this.Request.Params["currentPage"== null)
            
{
                currentpage 
= 1;
            }

            
else
            
{
                currentpage 
=Convert.ToInt32(Request.Params["currentPage"]);
            }

            bbb();
            
//string kk = PageData();
            
//Response.Write(kk);
           
        }

    }


    
public static int pages;               //总的页数
   
    
public static int pagecout;             //总的记录条数
    public static int  currentpage;                 //当前页
    
    
protected string PageData()
    
{
        
        
int cout=10;                       //每页要显示的条数
        int showpage=10;                   //数字导航条要显示页码的个数
        string leftInfo;
        pagecout 
= CalculateRecord();
        
if (pagecout % cout == 0)
        

            pages 
= pagecout / cout; 
        }

        
else
        
{
            pages 
= pagecout / cout + 1;

        }

        StringBuilder centerInfo 
= new StringBuilder();
      

        
//分页条分三部分,leftInfo是最左边的部分,用来显示当前页/总页数,每页显示的记录条数

        leftInfo 
= "" + currentpage +""+ "/" + ""+pages+"" + "  " + "每页"
            
+ cout + "" + "  共" + pagecout + "";
       
        
//中间的部分是分页部分

        
int min;//要显示的页面数最小值

        
int max;//要显示的页面数最大值

        
if (currentpage > pages)//当前页必须小于最大页
        {
            currentpage 
= pages;
        }


        
if (currentpage % showpage == 0//如果恰好整除
        {

            min 
= currentpage + 1;

            max 
= currentpage + showpage;
        }

        
else if (currentpage % showpage == 1 && currentpage > showpage)
        
{

            min 
= (((int)currentpage / showpage) - 1* showpage + 1;

            max 
= currentpage - 1;

        }


        
else
        
{

            min 
= ((int)currentpage / showpage) * showpage + 1;

            max 
= (((int)currentpage / showpage) + 1* showpage;

        }


        
string numberStr = " ";//循环生成数字序列部分

        
string AbsUrl;//(URL?)左边的部分

        AbsUrl 
= this.Context.Request.Url.ToString();

        
if (AbsUrl.IndexOf("?"== -1)
        
{

        }


        
else
        
{

            AbsUrl 
= AbsUrl.Substring(0, AbsUrl.IndexOf("?"));

        }



        
for (int i = min; i <= max; i++)
        
{

            
if (i <= pages)//只有不大于最大页才显示
            {

                
if (currentpage == i)//如果是当前页,用斜体和红色显示
                {

                    numberStr 
= numberStr + "<a href=" + AbsUrl + "?currentPage=" + i.ToString() + ">" + "<I style='color:red'>" + i.ToString() + "</I>" + "</a>" + "\n";

                }


                
else
                
{

                    numberStr 
= numberStr + "<a href=" + AbsUrl + "?currentPage=" + i.ToString() + ">" + i.ToString() + "</a>" + "\n";

                }


            }


        }



        
//第一页,上一页,下一页,最后一页

        
string First, Previous, Next, Last;

        First 
= AbsUrl + "?currentPage=1";

        
/////////

        
if (currentpage == 1)

            Previous 
= AbsUrl + "?currentPage=1";

        
else

            Previous 
= AbsUrl + "?currentPage=" + (currentpage - 1).ToString();

        
/////////

        
if (currentpage == pages)

            Next 
= AbsUrl + "?currentPage=" + pages;

        
else

            Next 
= AbsUrl + "?currentPage=" + (currentpage + 1).ToString();
        
/////////

        Last 
= AbsUrl + "?currentPage=" + pages;

        centerInfo.AppendFormat(
"<font face='Webdings' style='font-size:14px'><a href="/{0"}>7</a><a href="/{1"}>3</a></font>{2}<font face='Webdings' style='font-size:14px'><a href="/{3"}>4</a><a href="/{4"}>8</a></font>", First, Previous, numberStr, Next, Last);

        StringBuilder sb 
= new StringBuilder();//输出HTML字符串

        sb.AppendFormat(
"<table style = 'font-size:12px' border='0' cellpadding='0' cellspacing='0' width='100%'> \n " +

                        
"<tr>\n" +

                        
"<td width='25%' align='left'>{0}</td>\n" +

                        
"<td width='61%' align='right'>{1}</td>\n" +

                        
"<td width='14%' align='right'><input type='text' name='T1' size='4' style='border-bottom:solid 1pt gray;border-top :solid 1pt gray; border-left:solid 1pt gray;border-right:solid 1pt gray;'> \n    <input type='button' name='B1' size='6' value=go style='border-bottom:solid 1pt gray;border-top :solid 1pt gray; border-left:solid 1pt gray;border-right:solid 1pt gray;' onclick='go(T1,{2})'></td>\n" +

                        
"</tr>\n" +

                        
"</table>", leftInfo,

                        centerInfo.ToString(),

                        pages);

       
        
return sb.ToString();
    }



    
//ICollection CreateSource()
    
//{

    
//}

    
public int CalculateRecord()                //计算总共有多少条记录
    {
        
int allcout =0;
        
string sql = "select count(namee) as co from product";
   
       SqlDataReader   rs 
=(SqlDataReader) DBManager.SystemDatabaseExec.ExecReader(sql);
        
if (rs.Read())
        
{
            allcout 
= Convert.ToInt32(rs["co"].ToString());
        }

        DBManager.SystemDatabaseExec.Close();
        
return allcout;
    }

   
    
private void bbb()//从数据库读出数据,绑定,在页面显示
    {
        StringBuilder sql 
= new StringBuilder();
        
string sql1;
       
            sql.AppendFormat(
"select top {0} productid,productno,namec,namee from product where productid not in (select top {1} productid from product )"10, (currentpage-1* 10);
            sql1 
= sql.ToString();
      
        DataSet ds 
= new DataSet();
        
bool bol = DBManager.SystemDatabaseExec.ExecDataset(sql1, ds, "table");
        
if (!bol)
            
return;
        datalist1.DataSource 
= ds;
        datalist1.DataBind();
        DBManager.SystemDatabaseExec.Close();
    }

}