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>

 

posted @ 2015-01-28 10:20  ChineseMoonGod  阅读(198)  评论(0编辑  收藏  举报