zoukankan      html  css  js  c++  java
  • linqtosql 实现数据分页

    cs代码

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Web;
    using System.Web.UI;
    using System.Web.UI.WebControls;
    using System.Configuration;
    
    public partial class Default4 : System.Web.UI.Page
    {
        int pagesize = 200;
        protected void Page_Load(object sender, EventArgs e)
        {
    
    
            if (!IsPostBack)
            {
                ViewState["PageIndex"] = 0;
                bind();
            }
    
          
        }
        protected void bind()
        {
            int pageindex=Convert.ToInt32(ViewState["PageIndex"]);
            DataClasses3DataContext dc = new DataClasses3DataContext(ConfigurationManager.ConnectionStrings["SoyErpConnectionString"].ConnectionString.ToString());
            var result = (from r in dc.MA_Lot
                          select r).Skip(pagesize * pageindex).Take(pagesize);
            GridView1.DataSource = result;
            GridView1.DataBind();
            btnFirst.Enabled = true;
            btnPre.Enabled = true;
            btnNext.Enabled = true;
            btnLast.Enabled = true;
            if (pageindex == 0)
            {
                btnFirst.Enabled = false;
                btnPre.Enabled = false;
            }
            if (pageindex == getCount()-1)
            {
                btnNext.Enabled = false;
                btnLast.Enabled = false;
            }
        }
        protected int getCount()
        {
            DataClasses3DataContext dc = new DataClasses3DataContext(ConfigurationManager.ConnectionStrings["SoyErpConnectionString"].ConnectionString.ToString());
            int s1 = dc.MA_Lot.Count();
            int s2 = s1 % pagesize == 0 ? 0 : 1;
            return s1 / pagesize + s2;
        }
        protected void btnFirst_Click(object sender, EventArgs e)
        {
            ViewState["PageIndex"] = 0;
            bind();
        }
        protected void btnPre_Click(object sender, EventArgs e)
        {
            ViewState["PageIndex"] = Convert.ToInt32(ViewState["PageIndex"])-1; 
            bind();
        }
        protected void btnNext_Click(object sender, EventArgs e)
        {
            ViewState["PageIndex"] = Convert.ToInt32(ViewState["PageIndex"]) + 1;
            bind();
        }
        protected void btnLast_Click(object sender, EventArgs e)
        {
            ViewState["PageIndex"] = getCount()-1;
            bind();
        }
    }

    aspx前台代码

    <%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default4.aspx.cs" Inherits="Default4" %>
    
    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
    
    <html xmlns="http://www.w3.org/1999/xhtml">
    <head runat="server">
        <title></title>
    </head>
    <body>
        <form id="form1" runat="server">
            <div>
            <asp:Button ID="btnFirst" runat="server" Text="首页" onclick="btnFirst_Click" /> 
            <asp:Button ID="btnPre" runat="server" Text="上一页" onclick="btnPre_Click" />
            <asp:Button ID="btnNext" runat="server" Text="下一页" onclick="btnNext_Click" />
            <asp:Button ID="btnLast" runat="server" Text="尾页" onclick="btnLast_Click" /></div>
        <div>
            <asp:GridView ID="GridView1" runat="server">
            </asp:GridView>
        </div>
    
        </form>
    </body>
    </html>
  • 相关阅读:
    【Linux】【Shell】【Basic】文件查找locate,find
    【Linux】【Shell】【text】Vim
    【Linux】【Shell】【text】文本处理工具
    【Linux】【Shell】【text】grep
    【Linux】【Basis】用户、组和权限管理
    什么是高并发;超发的解决思路(悲观锁与乐观锁);高并发与多线程的关系--持续更新(十四)
    线程池的应用(十三)
    线程池基本概念(十二)
    ThreadLocal(十一)
    tomcat的单例多线程代码示例(十)
  • 原文地址:https://www.cnblogs.com/ChineseMoonGod/p/4255129.html
Copyright © 2011-2022 走看看