使用三层架构搭建数据分页(有刷新版)

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 = "请输入合法数字";
            }
        }
    }

 

 

 

 

 

 

 

 

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值