ASP.NET WEB中三层架构使用Repeater控件实现的真分页效果(详细代码案例)

在DBHelper中添加下面代码 查询有多少条数据

public static int Scalar(string sql)
    {
        conn.Open();
        SqlCommand comm = new SqlCommand(sql, conn);
        int num =(int) comm.ExecuteScalar();
        conn.Close();
        return num;
   }

在DAL中添加方法

 //查询分页数据
  public static DataTable GetFoodsByPage(int pageIndex,int pageSize, string condition="1=1")
  {
          string sql = $"select top {pageSize} * from food where {condition} and foodid not in (select top {(pageIndex - 1) * pageSize} foodid from food where {condition} order by foodid)";
            return DBHelper.GetData(sql);
  }

    //查询总记录数
  public static int GetFoodByCount()
  {
          string sql = "select count(*) from food";
          return DBHelper.Scalar(sql);
  }

在BLL中添加方法

 //  分页查询
        public static List<Food> GetFoodsByPage(int pageIndex, int pageSize, string condition = "1=1")
        {
            DataTable dt = FoodDAL.GetFoodsByPage(pageIndex,pageSize,condition);
            List<Food> list = new List<Food>();
            foreach (DataRow dr in dt.Rows)
            {
                Food food = new Food();
                food.FoodID = int.Parse(dr["FoodID"].ToString());
                food.FoodName = dr["FoodName"].ToString();
                food.FoodTypeID = int.Parse(dr["FoodTypeID"].ToString());
                food.Price = decimal.Parse(dr["Price"].ToString());
                food.PicUrl = dr["PicUrl"].ToString();
                food.SalesVolume = int.Parse(dr["SalesVolume"].ToString());
                food.AddDate = DateTime.Parse(dr["AddDate"].ToString());
                food.Desc = dr["Desc"].ToString();
                list.Add(food);
            }
            return list;
        }

//查询记录数
        public static int GetFoodByCount()
        {

            return FoodDAL.GetFoodByCount();
        }

创建类记录所有的分页操作:

 [Serializable]
    public class PubPager
    {
        public int PageIndex { get; set; }  //页号
        public int PageSize { get; set; }  //页容量
        public int PageCount { get; set; } //页总数
                                           //数据记录总数

        private int count;
        public int Count
        {
            get { return count; }
            set
            {
                this.PageCount = Convert.ToInt32(Math.Ceiling(double.Parse(value.ToString()) / double.Parse(PageSize.ToString())));
                this.count = value;
            }
        }


        public PubPager()
        {
            PageSize = 10;
            PageIndex = 1;
            Count = 0;
        }

        //是否是首页
        public bool CheckFirst()
        {
            return this.PageIndex - 1 <= 1;
        }
        //是否是尾页
        public bool CheckLast()
        {
            return this.PageIndex + 1 >= this.PageCount;
        }
        //上一页
        public void GoPrevPage()
        {
            if (this.CheckFirst())
                this.PageIndex = 1;
            else
                this.PageIndex--;
        }

        //下一页
        public void GoNextPage()
        {
            if (this.CheckLast())
                this.PageIndex = this.PageCount;
            else
                this.PageIndex++;
        }
        //第一页
        public void GoFirstPage()
        {
            this.PageIndex = 1;
        }

        //最后一页
        public void GoLastPage()
       {
            this.PageIndex =this.PageCount ;
        }

在Repeater控件的前端代码中添加

<asp:Label ID="labMessage" runat="server" Text="Label"></asp:Label>
            <asp:LinkButton ID="lbFirst" runat="server" OnClick="lbFirst_Click">首页</asp:LinkButton>
            <asp:LinkButton ID="lbPrev" runat="server" OnClick="lbPrev_Click">上一页</asp:LinkButton>
            <asp:LinkButton ID="lbNext" runat="server" OnClick="lbNext_Click">下一页</asp:LinkButton>
            <asp:LinkButton ID="lbLast" runat="server" OnClick="lbLast_Click">尾页</asp:LinkButton>

后端代码中添加:

PubPager pager;
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                pager = new PubPager();
                ViewState["page"] = pager;
               
                DataBinder(pager);
            }
            pager = ViewState["page"] as PubPager;   
     }
      
       
     
        private void DataBinder(PubPager pager)
        {
            string condition = "1=1";
            pager.Count = FoodBLL.GetFoodByCount();
            labMessage.Text = $"共{pager.Count}条数据;&emsp;每页{pager.PageSize}条.第{pager.PageIndex}页/共{pager.PageCount}页";
            rep.DataSource = FoodBLL.GetFoodsByPage(pager.PageIndex,pager.PageSize, condition);
            rep.DataBind();
        }








        //搜索查询方法
        protected void btnSreach_Click(object sender, EventArgs e)
        {
            string foodName = txtFoodName.Text; //获取文本框的值
            rep.DataSource = FoodBLL.GetFoodByName(foodName);//调用模糊查询方法
            rep.DataBind();
        }

        protected void lbFirst_Click(object sender, EventArgs e)
        {
            pager.GoFirstPage();
            DataBinder(pager);
        }

        protected void lbPrev_Click(object sender, EventArgs e)
        {
            pager.GoPrevPage();
            DataBinder(pager);
        }

        protected void lbNext_Click(object sender, EventArgs e)
        {
            pager.GoNextPage();
            DataBinder(pager);
        }

        protected void lbLast_Click(object sender, EventArgs e)
        {
            pager.GoLastPage();
            DataBinder(pager);
        }
  • 5
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Code_徐

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值