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>
  • 相关阅读:
    【转】Java中super和this的几种用法与区别
    公寓上网新认证方式破解研究
    移动设计
    破解哈工程大学深澜认证路由器
    Arch Linux安装记录
    foreach新解
    工具大全(转载)
    Linux 入门
    设置为驼峰命名
    efcore Add-Migration 错误
  • 原文地址:https://www.cnblogs.com/ChineseMoonGod/p/4255129.html
Copyright © 2011-2022 走看看