在C#中每10条评论后如何进行分页

时间:2012-09-21 13:55:40

标签: c# asp.net .net pagination

我有一份评论清单。客户在每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的新手,所以任何帮助都会受到赞赏。

提前致谢!

2 个答案:

答案 0 :(得分: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),但我对此站点上建议的任何插件都没有经验。

答案 1 :(得分:1)

看看这个

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

希望这有帮助