求好用的分页代码(不用服务器控件)
一般的分页控件都是通过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一下 会有很多可用的。
[解决办法]
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 + "页 "; retstr += ("Total " + total + " " + pageindex + "/" + pagecount + " 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++) { retstr += pageindex == i ? "<strong>" + i + "</strong>" : "<a href=\"" + url + "Page=" + i + "\">" + i + "</a>"; } retstr += pageindex != pagecount ? "<a href=\"" + url + "Page=" + next + "\">Next</a><a href=\"" + url + "Page=" + pagecount + "\">Last</a>" : ""; return retstr; }
[解决办法]
using System.Text;
using System.Windows.Forms;
namespace Demo
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
int Page = 1;//窗体加载时显示第1页
int N = 2;//默认每页显示2条
public void bangding(int N, int P)
{
if (this.listView1.Items.Count != 0)
this.listView1.Items.Clear();
ArrayList list = DBHelper.ExecuteSel(N, P);
if (list.Count != 0)
{
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 });
}
}
else
{
MessageBox.Show("没有记录!");
}
}
private void Form1_Load(object sender, EventArgs e)
{
//string sql = "select EmpNo,EmpName,EmpAddress,DeptNo from Employee";
//SqlDataReader reader =DBHelper.ExecuteSelect(sql);
this.label3.Text = "当前第"+Page+"页";
bangding(this.N, this.Page);
}
//上一页
private void button1_Click(object sender, EventArgs e)
{
if (this.Page == 1)
{
MessageBox.Show("这已经是第1页了!");
}
else
{
this.Page = this.Page - 1;
bangding(this.N, this.Page);
this.label3.Text = "当前第" + Page + "页";
}
}
//下一页
private void button2_Click(object sender, EventArgs e)
{
this.Page = this.Page + 1;
bangding(this.N, this.Page);
this.label3.Text = "当前第" + Page + "页";
}
//更改每页显示多少条
private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
{
this.N = int.Parse(this.comboBox1.Text.ToString());
}
}
}
[解决办法]
private void GetSearchList() { PageDataAcc pageList = new PageDataAcc(); pageList.SearchType = m_PageType; pageList.SearchWhere = m_Where; pageList.TableName = "CMS_Contents"; pageList.PageNo = int.Parse(m_PageNo); pageList.PageSize = 10; pageList.PrimaryKey = "CmsID"; pageList.Url = "pageSearch.aspx"; pageList.SearchKeys = m_SearchKey; DataSet pageDs = new DataSet(); string catchMsg = string.Empty; string navUrl = string.Empty; string title = string.Empty; DataRow[] pageDr; pageDs = pageList.GetPageList(ref catchMsg); pageDr = new DataRow[pageDs.Tables[0].Rows.Count]; pageDr = pageDs.Tables[0].Select(); int rowLen = pageDr.Length; pageList.PageCount = rowLen; StringBuilder sHtml = new StringBuilder(); for (int i = 0; i < rowLen; i++) { title = pageDr[i]["CmsTitle"].ToString(); title = PageValidate.CutString(title, 30); if (m_PageType == "1") { navUrl = "newsValue.aspx?id=" + pageDr[i]["CmsID"].ToString() + ""; } else { navUrl = "proValue.aspx?id=" + pageDr[i]["CmsID"].ToString() + ""; } sHtml.Append("<li class=\"title\"><div class=\"title\">"); sHtml.Append("<a href=\"" + navUrl + "\" target=\"_blank\">"); sHtml.Append("" + pageDr[i]["CmsTitle"] + "</a></div>"); sHtml.Append("<div class=\"time\">" + Convert.ToDateTime(pageDr[i]["OprateDate"]).ToString("yyyy年MM月dd日") + "</div></li>"); } LiteralDataList.Text = sHtml.ToString(); Literal1.Text = pageList.GetPageNavStrTB(10);