一个简单的分页程序(通用且适合初学者)
很久以前写的一个简单的分页演示程序,此方法面对数据量非常的时候就不太适用,但适合初学者参考一下。
这里是粗略模拟一个数据层。并未细分。希望大家指正
public class conn
{
static int count;//存储总记录的变量
public static int pageCount;//存储总页数的变量
public static DataTable connto(int param)//此方法利用dataReader方法绑定数据至DataGrid
{ //dataReader方法比DataSet读取效率更高。但是不能对数据修改和任意访问
SqlConnection cn = new SqlConnection(System.Configuration.ConfigurationSettings.AppSettings["cnToSql"]);
int i = (param-1)*5;
string str = "select top 5 * from jobs where job_id not in"+
" (select top "+i+" job_id from jobs order by job_id)order by job_id";
SqlDataAdapter da = new SqlDataAdapter(str,cn);
DataSet ds = new DataSet();
da.Fill(ds,"job");
cn.Open();
SqlCommand cmd = new SqlCommand("select count(job_id) from jobs",cn);
count = (int)cmd.ExecuteScalar();
pageCount = count%5==0?count/5:count/5+1;
cn.Close();
return ds.Tables["job"];
}
}
以下代码为ASPX页面中调用。{
static int count;//存储总记录的变量
public static int pageCount;//存储总页数的变量
public static DataTable connto(int param)//此方法利用dataReader方法绑定数据至DataGrid
{ //dataReader方法比DataSet读取效率更高。但是不能对数据修改和任意访问
SqlConnection cn = new SqlConnection(System.Configuration.ConfigurationSettings.AppSettings["cnToSql"]);
int i = (param-1)*5;
string str = "select top 5 * from jobs where job_id not in"+
" (select top "+i+" job_id from jobs order by job_id)order by job_id";
SqlDataAdapter da = new SqlDataAdapter(str,cn);
DataSet ds = new DataSet();
da.Fill(ds,"job");
cn.Open();
SqlCommand cmd = new SqlCommand("select count(job_id) from jobs",cn);
count = (int)cmd.ExecuteScalar();
pageCount = count%5==0?count/5:count/5+1;
cn.Close();
return ds.Tables["job"];
}
}
窗体LOAD事件中加载数据。
static int pageIndex=1;//初试的页索引
private void Page_Load(object sender, System.EventArgs e)
{
this.Page.EnableViewState = false;
if (!IsPostBack)
{
DataGrid1.DataSource = conn.connto(pageIndex);
DataGrid1.DataBind();
Label2.Text = "当前为第"+ pageIndex +"页";
}
Label1.Text = "共有" + conn.pageCount.ToString() + "页";
}
private void Page_Load(object sender, System.EventArgs e)
{
this.Page.EnableViewState = false;
if (!IsPostBack)
{
DataGrid1.DataSource = conn.connto(pageIndex);
DataGrid1.DataBind();
Label2.Text = "当前为第"+ pageIndex +"页";
}
Label1.Text = "共有" + conn.pageCount.ToString() + "页";
}
这个方法做什么用的就不说咯
void disable()
{
if (pageIndex == 1)
{
LinkButton1.Enabled = false;
LinkButton2.Enabled = false;
}
else
{
LinkButton1.Enabled = true;
LinkButton2.Enabled = true;
}
if (pageIndex == conn.pageCount)
{
LinkButton3.Enabled = false;
LinkButton4.Enabled = false;
}
else
{
LinkButton3.Enabled = true;
LinkButton4.Enabled = true;
}
}
四个按扭{
if (pageIndex == 1)
{
LinkButton1.Enabled = false;
LinkButton2.Enabled = false;
}
else
{
LinkButton1.Enabled = true;
LinkButton2.Enabled = true;
}
if (pageIndex == conn.pageCount)
{
LinkButton3.Enabled = false;
LinkButton4.Enabled = false;
}
else
{
LinkButton3.Enabled = true;
LinkButton4.Enabled = true;
}
}
private void LinkButton1_Click(object sender, System.EventArgs e)
{
pageIndex = 1;
DataGrid1.DataSource = conn.connto(pageIndex);
DataGrid1.DataBind();
Label2.Text = "当前为第"+ pageIndex +"页";
disable();
}
private void LinkButton4_Click(object sender, System.EventArgs e)
{
pageIndex = conn.pageCount;
DataGrid1.DataSource = conn.connto(pageIndex);
DataGrid1.DataBind();
Label2.Text = "当前为第"+ pageIndex +"页";
disable();
}
private void LinkButton2_Click(object sender, System.EventArgs e)
{
pageIndex--;
DataGrid1.DataSource = conn.connto(pageIndex);
DataGrid1.DataBind();
Label2.Text = "当前为第"+ pageIndex +"页";
disable();
}
private void LinkButton3_Click(object sender, System.EventArgs e)
{
pageIndex++;
DataGrid1.DataSource = conn.connto(pageIndex);
DataGrid1.DataBind();
Label2.Text = "当前为第"+ pageIndex +"页";
disable();
}
{
pageIndex = 1;
DataGrid1.DataSource = conn.connto(pageIndex);
DataGrid1.DataBind();
Label2.Text = "当前为第"+ pageIndex +"页";
disable();
}
private void LinkButton4_Click(object sender, System.EventArgs e)
{
pageIndex = conn.pageCount;
DataGrid1.DataSource = conn.connto(pageIndex);
DataGrid1.DataBind();
Label2.Text = "当前为第"+ pageIndex +"页";
disable();
}
private void LinkButton2_Click(object sender, System.EventArgs e)
{
pageIndex--;
DataGrid1.DataSource = conn.connto(pageIndex);
DataGrid1.DataBind();
Label2.Text = "当前为第"+ pageIndex +"页";
disable();
}
private void LinkButton3_Click(object sender, System.EventArgs e)
{
pageIndex++;
DataGrid1.DataSource = conn.connto(pageIndex);
DataGrid1.DataBind();
Label2.Text = "当前为第"+ pageIndex +"页";
disable();
}
搞定