Linq+AspNetPager+Gridview的结合使用

XHTML结构:

<asp:GridView ID="GridView1" runat="server" CssClass="tbtitle acenter" Width="100%" 
    BackColor="#E2F5BC" BorderWidth="0" CellPadding="1" CellSpacing="1" >
</asp:GridView>
<!-- Pagination -->
<webdiyer:AspNetPager Width="100%" ID="anpager" runat="server"
        FirstPageText="首页 |"
        LastPageText="| 尾页" 
        PrevPageText="上一页 |"
        NextPageText="| 下一页"
        OnPageChanged="anpager_PageChanged" 
        PageIndexBoxType="DropDownList" 
        ShowPageIndexBox="Always" 
        TextAfterPageIndexBox=" 页" TextBeforePageIndexBox="第 "
        NumericButtonCount="10" 
        ShowPageIndex="true" 
        ShowBoxThreshold="10" 
        ShowNavigationToolTip="True" 
        PageSize="12"
        AlwaysShow="true" UrlPaging="false">
</webdiyer:AspNetPager>

.cs(C#):

private void BindList()
{
    List<tbSalesOrderHeader> list = new List<tbSalesOrderHeader>();
    list = BSalesOrderHeader.GetList(anpager.CurrentPageIndex, anpager.PageSize);
    if (list.Count > 0)
    {
        anpager.RecordCount = BSalesOrderHeader.GetCount(); //读取所有符合的记录数
        gvList.DataSource = list;
        gvList.DataBind();
    }
}

protected void anpager_PageChanged(object sender, EventArgs e)
{
    BindList();
}

  

封装的类:

    public class BSalesOrderHeader
    {
        private static SalesOrderHeaderDataContext dc = Linq2SqlInstance.SalesOrderHeader;

        public static List<tbSalesOrderHeader> GetList(int iCurrentIndex, int iPageSize)
        {
            return dc.tbSalesOrderHeader.Skip((iCurrentIndex - 1) * iPageSize).Take(iPageSize).ToList<tbSalesOrderHeader>();
        }

        public static int GetCount()
        {
            return dc.tbSalesOrderHeader.Count();
        }
    }

其中,关键点是:Skip((AspNetPager.CurrentPageIndex - 1) * AspNetPager.PageSize).Take(AspNetPager.PageSize); 

posted @ 2011-09-23 14:45  沙茶叶  阅读(284)  评论(0编辑  收藏  举报