日期:2014-05-18  浏览次数:20479 次

求好用的分页代码(不用服务器控件)
一般的分页控件都是通过form传值,谁有拼凑的html,后台替换链接。的分页代码


------解决方案--------------------
using System.Collections;

ArrayList类:集合类
ArrayList list=new ArrayList();
//添加元素
list.Add(Object obj);


public class Employee
{
public string EmpNo;
public string EmpName;
public string EmpAddress;
public string DeptNo;
}

public ArrayList ExecuteSelect(string sql)
{
//创建一个集合对象
ArrayList list=new ArrayList();

try
{
SqlCommand cmd=new SqlCommand(sql,conn);
conn.Open();

SqlDataReader reader=cmd.ExecuteReader();

if(reader.HasRows)
{
while(reader.Read())
{
//读取一行,把当前行放入Employee对象中
Employee emp=new Employee();
emp.EmpNo=reader["EmpNo"].ToString();
emp.EmpName=reader["EmpName"].ToString();
emp.EmpAddress=reader["EmpAddress"].ToString();
emp.DeptNo=reader["DeptNo"].ToString();
//将此对象,放入集合中
list.Add(emp);

}
}
reader.Close();
}catch(Exception ee)
{
}
finally
{
conn.Close();
}

return list;
}

1将读取的行--->2放入对象中---->3集合中------>窗体的ListView控件
ArrayList list=new ArrayList();
.....
1.逐行逐列的读取
if(reader.HasRows)
{
while(reader.Read())
{
//2.构建对象
Employee emp=new Employee();
emp.EmpNo=reader["EmpNo"].ToString();
.....

//3.
list.Add(emp);
}
}
reader.Close();
.....
//4.窗体中显示
//循环遍历集合中的元素,将每个对象[一行记录],添加到控件
....
ArrayList list=DBHelper.XXXX(sql);
....
foreach(Employee emp in list)
{
//每一个元素就是一行记录
ListViewItem item=new ListViewItem(emp.EmpNo);
this.listView1.items.Add(item);

item.SubItems.AddRange(new string [] {emp.EmpName,
emp.EmpAddress,
emp.DeptNo});
}
//分页
select top N * from 表 where 列 not in(select top N*(P-1) 列 from 表)
//N 每页多少行记录
//P 当前第几页




















------解决方案--------------------
google一下 会有很多可用的。
------解决方案--------------------
C# code
public static string Pagination(int total, int pageindex, int pagecount, string url)
        {
            if (url.IndexOf("?") >= 0)
                url += "&";
            else
                url += "?";
            int next = 0;
            int pre = 0;
            int startcount = 0;
            int endcount = 0;
            string retstr = string.Empty;
            if (pageindex < 1) pageindex = 1;
            if (pageindex > pagecount) pageindex = pagecount;
            next = pageindex + 1;
            pre = pageindex - 1;
            startcount = (pageindex + 5) > pagecount ? pagecount - 9 : pageindex - 4;
            //中间页终止序号
            endcount = pageindex < 5 ? 10 : pageindex + 5;
            if (startcount < 1) { startcount = 1; }
            if (pagecount < endcount) { endcount = pagecount; }
            //currentpagestr = "共" + pagecount + "页&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;";
            retstr += ("Total&nbsp;&nbsp;" + total + " " + pageindex + "/" + pagecount + "&nbsp;&nbsp;Page  ");
            retstr += pageindex > 1 ? "<a href=\"" + url + "Page=1\">First</a><a href=\"" + url + "Page=" + pre + "\">Prev</a>" : "";

            for (int i = startcount; i <= endcount; i++)
            {