Linq to shql 自定义分页示例

前台代码:

<%@ Page Language="C#" AutoEventWireup="true"  CodeFile="Default.aspx.cs" Inherits="_Default" %>

<!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:GridView ID="GridView1" runat="server"
            DataKeyNames="Id">
            <PagerSettings Visible="False" />
        </asp:GridView>
    </div>
    <asp:Label ID="Label1" runat="server" Text="Label"></asp:Label>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
    <asp:Label ID="Label2" runat="server" Text="Label"></asp:Label>
    <br />
    <asp:LinkButton ID="LinkButton1" runat="server" onclick="LinkButton1_Click">首页</asp:LinkButton>
&nbsp;&nbsp;
    <asp:LinkButton ID="LinkButton2" runat="server" onclick="LinkButton2_Click">上一页</asp:LinkButton>
&nbsp;&nbsp;
    <asp:LinkButton ID="LinkButton3" runat="server" onclick="LinkButton3_Click">下一页</asp:LinkButton>
&nbsp;&nbsp;
    <asp:LinkButton ID="LinkButton4" runat="server" onclick="LinkButton4_Click">尾页</asp:LinkButton>
    <br />
    </form>
</body>
</html>

后台代码:

using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Xml.Linq;

public partial class _Default : System.Web.UI.Page
{
    DataConnectionDataContext db = new DataConnectionDataContext();
    int pagesize = 5;//每页显示多少条记录
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            ViewState["pageindex"] = 0;
            newsbind();
            Label1.Text = num().ToString() + "记录";
            Label2.Text = pagecount(pagesize).ToString() + "页";
        }
    }
    //总记录数
    private int num()
    {
        int count=from n db.News.Count();

        return count;
    }
    //根据pagesize计算总页数
    private int pagecount(int x)
    {
        int count = (num() + x) / x;
        return count;
    }

    //绑定GridView
    private void newsbind()
    {
        int pageindex = Convert.ToInt32(ViewState["pageindex"].ToString());
       
        this.GridView1.DataSource = (from n in db.News
                                     orderby n.Id descending
                                     select new { n.Id, n.NewTitle, n.NewContext, n.NewImage, n.NewDate }).Skip(pagesize * pageindex).Take(pagesize);
        this.GridView1.DataBind();

       
        this.LinkButton2.Enabled = false;
        this.LinkButton3.Enabled = false;
        if (Convert.ToInt32(ViewState["pageindex"].ToString()) > 0)
        {
            this.LinkButton2.Enabled = true;
        }
        if (Convert.ToInt32(ViewState["pageindex"].ToString()) < pagecount(pagesize) -1 )
        {
            this.LinkButton3.Enabled = true;
        }
    }
    protected void LinkButton1_Click(object sender, EventArgs e)
    {
        ViewState["pageindex"] = 0;
        this.newsbind();
    }
    protected void LinkButton2_Click(object sender, EventArgs e)
    {
        ViewState["pageindex"] = Convert.ToInt32(ViewState["pageindex"].ToString()) - 1;
        this.newsbind();
    }
    protected void LinkButton3_Click(object sender, EventArgs e)
    {
        ViewState["pageindex"] = Convert.ToInt32(ViewState["pageindex"].ToString()) + 1;
        this.newsbind();
    }
    protected void LinkButton4_Click(object sender, EventArgs e)
    {
        ViewState["pageindex"] = pagecount(pagesize) - 1;
        this.newsbind();
    }
}

现在正在看linq 的分页,可是发现,怎么不能够设置我显示哪页啊。一直是显示我分页的最后有个页的内容。


代码如下

var q =( from c in textuser where c.id >0 orderby c.id descending select new { ID = c.id, Sex = c.Sex == 1 ? "男" : "女", c.UserName }).ToList().Take

(3).Skip(0);
            GridView1.DataSource = q;
            GridView1.DataBind();

如果我要想显示,第一页的 内容怎么显示。

顺便问下。我现在的理解是 Take 设置页面记录数,Skip 是从哪条记录开始显示,不知道我这样理解 正确不?
---
int _page = 1;
int _pagesize = 40;
var q =( from c in textuser where c.id >0 orderby c.id descending select new { ID = c.id, Sex = c.Sex == 1 ? "男" : "女", c.UserName });
GridView1.DataSource =q.Skip((_page - 1) * _pagesize).Take(_pagesize).ToList();
GridView1.DataBind();
posted @ 2008-06-13 14:54  barney  阅读(301)  评论(0编辑  收藏  举报