0

我有一个评论列表。客户希望每 10 条评论后有一个新页面,因此评论 11 将出现在第 2 页上,以此类推,直到他们获得的页面数。由于我在常规 .aspx.cs 页面上遇到的问题,因此在 .ashx 页面上形成了评论。简而言之,我该如何做到这一点?

这是 .ashx 页面的代码:

public void ProcessRequest (HttpContext context) 
{

    // ****************************************
    if (context.Request["postform"] == "1")
    {

        videomessage myVideoMessage = new videomessage();

        myVideoMessage.video_id = context.Request["video_id"];
        myVideoMessage.first_name_submitter = context.Request["first_name_submitter"];
        myVideoMessage.last_initial_submitter = context.Request["last_initial_submitter"];
        myVideoMessage.message = context.Request["message"];
        myVideoMessage.status = "0";

        myVideoMessage.Save();
    }
    // ****************************************

    // ****************************************
    StringBuilder myStringBuilder = new StringBuilder();


    // PULL VIDEOMESSAGES FOR VIDEO_ID
    videomessage[] myCommentsList = new videomessage().Listing("video_id", context.Request["video_id"], "entry_date" , "DESC");

    // FORM COMMENTS IF MORE THAN ONE COMMENT EXISTS
    foreach (videomessage tmpMessage in myCommentsList)
    {
        if (tmpMessage.status == "0" || tmpMessage.status == "1")
        {
            myStringBuilder.Append("<div class=\"comment_box\">");
            myStringBuilder.Append("<p class=\"comment_date\">");
            myStringBuilder.Append(Utility.FormatShortDate(tmpMessage.entry_date) + " " + tmpMessage.first_name_submitter + " " + tmpMessage.last_initial_submitter + "." + "</p>");

            if (!String.IsNullOrEmpty(tmpMessage.message))
            {
                myStringBuilder.Append("<p>" + tmpMessage.message + "</p>");
                myStringBuilder.Append("</div>");
            }
        }
    }
    string return_str = myStringBuilder.ToString();

    // IF NO COMMENTS RETURN THIS
    if( String.IsNullOrEmpty(return_str) )  return_str = "<p>No comments currently exist for this video.</p>";
    // ****************************************

    // RETURN STRING        
    context.Response.ContentType = "text/plain";
    context.Response.Write(return_str);
}

我知道需要涉及一些数学并将其分配给变量,但我对 .NET 仍然很陌生,因此将不胜感激。

提前致谢!

4

2 回答 2

2

使用 Linq

const int pageSize = 10;
var paginatedComments = comments.Skip((page ?? 0) * pageSize).Take(pageSize).ToList();

(*) 页面是从请求中输入的

可能有点过时了,但是看看 nerddinner 教程(http://aspnetmvcbook.s3.amazonaws.com/aspnetmvc-nerdinner_v1.pdf),它描述了使用自定义 PaginatedList 类的 MVC 分页原理

    public class PaginatedList<T> : List<T> {
        public int PageIndex  { get; private set; }
        public int PageSize   { get; private set; }
        public int TotalCount { get; private set; }
        public int TotalPages { get; private set; }

        public PaginatedList(IQueryable<T> source, int pageIndex, int pageSize) {
             PageIndex = pageIndex;
             PageSize = pageSize;
             TotalCount = source.Count();
             TotalPages = (int) Math.Ceiling(TotalCount / (double)PageSize);
             this.AddRange(source.Skip(PageIndex * PageSize).Take(PageSize));
        }

        public bool HasPreviousPage {
           get {
              return (PageIndex > 0);
           }
        }

        public bool HasNextPage {
            get {
               return (PageIndex+1 < TotalPages);
            }
        }
   }

替代解决方案是使用 jquery 方法(请参阅http://www.jquery4u.com/plugins/10-jquery-pagination-plugins),但我对本网站建议的任何一个插件都没有经验。

于 2012-09-21T14:35:10.077 回答
1

看看这个

currentPage=1
NextPageFirstRecord=(currrentpage-1)*noRecordsPerPage
pageCount=totalrecords/noRecordsPerPage

希望这可以帮助

于 2012-09-21T14:02:27.493 回答