点击下载源代码及控件:Rsion.ASP.NET.rar
新建一个分页类继承上面的类

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
namespace WgEdu.Helper
{
public class PagedDataSource<T>:
        Rsion.ASP.NET.Data.Linq.PagedDataSource<T> where T:class
    {
public PagedDataSource()
            : base(new Models.DbContext())
        {
        }
    }
}

这样就可以使用分页了
创建一个分页数据源对象:

PagedDataSource<EduBook> ps = new PagedDataSource<EduBook>();
            ps.CurrentPageIndex = (page??1)-1;//当前页,page为参数
            ps.PageSize = 1;//每页显示数目
            ps.IsDescendingSort = true;//是否倒序排列
            ps.OrderBy = a => a.AddDate;//根据字段排序
            ps.SelectObject = a => a;  //选择字段          ps.Condition = a => true; //符合选择的条件
//ps就做为一个数据源了。
//下面在mvc中返回这个数据源
return View(ps);

在View中将IView设置为

Inherits="System.Web.Mvc.ViewPage<WgEdu.Helper.PagedDataSource<要迭代的类型>>"
通过Model.PagedInfoContent属性获取分页控制和显示信息
下面是View的代码

<%@ Page Title="" Language="C#" MasterPageFile="~/Views/Shared/admin.Master" Inherits="System.Web.Mvc.ViewPage<WgEdu.Helper.PagedDataSource<WgEdu.Models.EduBook>>" %>
<asp:Content ID="Content1" ContentPlaceHolderID="TitleContent" runat="server">
    EduBooksList
</asp:Content>
<asp:Content ID="Content2" ContentPlaceHolderID="MainContent" runat="server">
<h2 class="controlnav">书籍列表</h2>
<br />
<table cellspacing="0" class="mgr">
<tr>
<th style="width:250px;">名称</th>
</tr>
<% foreach (var item in Model){ %>
<tr>
<td>
<%= Html.Encode(item.Name) %>
</td>
</tr>
<% }%>
</table>
      <!--分页信息显示在下面-->
<%=Model.PagedInfoContent %>
</asp:Content>

即实现了如下分页效果:

怎样是不是很简单呢??

posted on 2009-09-05 00:07  sinbad_li  阅读(748)  评论(0编辑  收藏  举报