zoukankan      html  css  js  c++  java
  • Gridview使用LINQ分页例子

    Linq的分页主要是使用了skip和take
    
    关于skip 参看 http://msdn.microsoft.com/zh-cn/library/bb357513.aspx
    
    关于take 参看http://msdn.microsoft.com/zh-cn/library/bb300906.aspx
    
    另外说明下NorthWindDataContext 我是采用了Linq的类成长办法自动生成的
    
    关于Linq的语法和介绍可以参看http://www.cnblogs.com/lovecherry/archive/2007/08/13/853754.html
    
    上面介绍的还不错。其实如果你用过SQL,用过枚举类型会发现LINQ没啥,但是确实很省力,会发现以前的ORM工具在小型开发的时候直接用LINQ算了。
    
    
    
    前台代码Default.aspx
    
    <%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %>
    
    <%@ Register assembly="AspNetPager" namespace="Wuqi.Webdiyer" tagprefix="webdiyer" %>
    
    <!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">
            </asp:GridView>
        
            <br />
            <webdiyer:AspNetPager ID="Pager" runat="server" 
                PageIndexBoxType="DropDownList" ShowPageIndexBox="Always" SubmitButtonText="Go" 
                TextAfterPageIndexBox="" TextBeforePageIndexBox="转到" 
                CurrentPageButtonPosition="End" 
                CustomInfoHTML="共%PageCount%页,当前为第%CurrentPageIndex%页,每页%PageSize%条" 
                FirstPageText="首页" LastPageText="尾页" NextPageText="下一页" 
                onpagechanged="Pager_PageChanged" PrevPageText="上一页">
            </webdiyer:AspNetPager>
        
        </div>
        </form>
    </body>
    </html>
    
    后台代码Default.aspx.cs
    
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Web;
    using System.Web.UI;
    using System.Web.UI.WebControls;
    
    public partial class _Default : System.Web.UI.Page 
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!Page.IsPostBack)
            {
                //得到所有的分页信息和预设的分页大小
                //pagesize来自girdview
                NorthWindDataContext context = new NorthWindDataContext();
                int total = (from custom in context.GetTable<Customers>()
                             select custom).Count();
                Pager.PageSize = GridView1.PageSize;
                Pager.RecordCount = total;
    
                gvDataBind(GridView1.PageSize, 1);
    
            }
        }
    
        protected void gvDataBind(int pagesize, int pageindex)
        {
            NorthWindDataContext context = new NorthWindDataContext();
            var customs = (from custom in context.GetTable<Customers>()
                           orderby custom.ContactName
                           select new
                           {
                               custom.ContactName,
                               custom.CustomerID,
                               custom.CompanyName,
                               custom.ContactTitle
    
                           }).Skip((pageindex - 1) * pagesize).Take(pagesize);
            GridView1.DataSource = customs;
            GridView1.DataBind();               
    
        }
    
    
        protected void Pager_PageChanged(object sender, EventArgs e)
        {
            GridView1.PageIndex = Pager.CurrentPageIndex;
            gvDataBind(GridView1.PageSize, Pager.CurrentPageIndex);
        }
    }
  • 相关阅读:
    禅知Pro 1.6 前台任意文件读取 | 代码审计
    wpa破解学习实践
    Natural Merge Sort(自然归并排序)
    [转]the service mysql57 failed the most recent status[/br]mysql57 was not found解决办法
    《Metasploit魔鬼训练营》第七章学习笔记
    Adobe阅读器漏洞(adobe_cooltype_sing)学习研究
    MS10_087漏洞学习研究
    第三方插件渗透攻击之KingView
    《Metasploit魔鬼训练营》虚拟环境搭建中网络配置的一些问题
    KingView 6.53漏洞学习研究
  • 原文地址:https://www.cnblogs.com/xw2cc1314/p/2971266.html
Copyright © 2011-2022 走看看