1.首先搭建三层的结构
BLL,ADL,DataAccess,UL
在表现层中数据调用BLL层中,BLL层是对数据层的操作,对数据逻辑的处理。在BLL层中调用DAL层,该层对数据的增删改查进行操作。
在BLL层中
private 分页操作.DAL.表名 dal=new 分页操作.DAL.表名 ();
public DataTable GetListDataTable(int pagesize, int pageindx)
{
return dal.GetListDataTable(pagesize,pageindx);
}
在DAL层
public DataTable GetListDataTable(int PageSize, int pageIndex)
{
SqlParameter[] parameters ={
new SqlParameter("@PageSize",SqlDbType.Int),
new SqlParameter("@pageIndex",SqlDbType.Int)
};
parameters[0].Value = PageSize;
parameters[1].Value = pageIndex;
return DbHelperSQL.RunProcedureDataTable("pro_fenye", parameters);
}
在DataAccess层 在DBHelperSQL.cs中
#region 自己写的分页
public static DataTable RunProcedureDataTable(string storedProcName, IDataParameter[] parameters)
{
using (SqlConnection connection = new SqlConnection(connectionString))
{
DataTable dt = new DataTable();
connection.Open();
SqlDataAdapter sqlDA = new SqlDataAdapter();
sqlDA.SelectCommand = BuildQueryCommand(connection, storedProcName, parameters);
sqlDA.Fill(dt);
connection.Close();
return dt;
}
}
#endregion
对于UI层的后台
public partial class fenye : System.Web.UI.Page
{
int pagesize = 10;
int pageindex = 1;
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
ViewState["pageindex"] = 1;
GetLastPageindex();
LoadData();
}
}
private void GetLastPageindex()
{
BLL.T_News1 bnews = new BLL.T_News1();
int totalcount = bnews.GetRecordCount("");
if (totalcount % pagesize == 0)
{
ViewState["lastpageindex"] = totalcount / pagesize;
}
else
{
ViewState["lastpageindex"] = totalcount / pagesize + 1;
}
}
private void LoadData()
{
BLL.T_News1 bnews = new BLL.T_News1();
DataTable dt = bnews.GetListDataTable(pagesize, Convert.ToInt32(ViewState["pageindex"]));
this.GridView1.DataSource = dt;
this.GridView1.DataBind();
}
protected void btnFirst_Click(object sender, EventArgs e)
{
ViewState["pageindex"] = 1;
LoadData();
}
protected void btnPre_Click(object sender, EventArgs e)
{
int pageindex = Convert.ToInt32(ViewState["pageindex"]);
if (pageindex > 1)
{
pageindex--;
ViewState["pageindex"] = pageindex;
LoadData();
}
}
protected void btnNext_Click(object sender, EventArgs e)
{
int pageindex = Convert.ToInt32(ViewState["pageindex"]);
if (pageindex < Convert.ToInt32(ViewState["lastpageindex"]))
{
pageindex++;
ViewState["pageindex"] = pageindex;
LoadData();
}
}
protected void btnLast_Click(object sender, EventArgs e)
{
ViewState["pageindex"]=ViewState["lastpageindex"];
LoadData();
}
protected void LinkButton5_Click(object sender, EventArgs e)
{
int result;
if (int.TryParse(txtPageindex.Text, out result) == true)
{
ViewState["pageindex"] = txtPageindex.Text.Trim();
LoadData();
}
else {
txtPageindex.Text = "请输入合法数字";
}
}
}