Post comment paging

Topics: Controls
Sep 28, 2010 at 7:12 PM
Edited Sep 28, 2010 at 7:55 PM

Hi everyone,

I'm trying to add paging or a "view older comments" to the post comments. I'm using version 1.5. I'm re-using the paging code from the PostList control and adding it to PostCommentView. Here's the code I added/changed in PostCommentView. It isn't working and I can't figure out why. Using paging here may also cause a problem when the recursive call is made for the nested comments. A "view older comments" link might be a better way to go where the older posts are appended to the placeholder. Either would be acceptable.

Can someone please help me?

private void AddNestedComments(string path, List<PostComment> nestedComments, PlaceHolder phComments)
    {
        if (nestedComments == null || nestedComments.Count == 0)
        {
            hlPrev.Visible = false;
            return;
        }

        int count = Math.Min(20, nestedComments.Count);
        int page = GetPageIndex();
        int index = page * count;
        int stop = count;
        if (index + count > nestedComments.Count)
            stop = nestedComments.Count - index;

        if (stop < 0 || stop + index > nestedComments.Count)
        {
            hlPrev.Visible = false;
            hlNext.Visible = false;
            return;
        }

        int counter = 0;

        foreach (PostComment comment in nestedComments.GetRange(index, stop))
        {
            if (counter == stop)
                break;

            PostCommentViewBase control = (PostCommentViewBase)LoadControl(path);
            if (comment.IsApproved || !Settings.Instance.EnableCommentsModeration || (!comment.IsApproved && Page.User.Identity.IsAuthenticated))
            {
                control.WallComment = comment;
                control.Wall = Wall;

                if (comment.Comments.Count > 0)
                {
                    // find the next placeholder and add the subcomments to it
                    PlaceHolder phSubComments = control.FindControl("phSubComments") as PlaceHolder;
                    if (phSubComments != null)
                    {
                        AddNestedComments(path, comment.Comments, phSubComments);
                    }
                }

                phComments.Controls.Add(control);
            }

            counter++;
        }

        if (index + stop == nestedComments.Count)
            hlPrev.Visible = false;
    }
private int GetPageIndex()
    {
        int index = 0;
        string page = Request.QueryString["page"];
        if (page != null && int.TryParse(page, out index) && index > 0)
            index--;

        return index;
    }

private void InitPaging()
    {
        string path = Request.RawUrl;

        // Leave "default.aspx" when posts for a specific year/month or specific date are displayed.
        //if (!(Request.QueryString["year"] != null || Request.QueryString["date"] != null))
        //    path = REMOVE_DEFAULT_ASPX.Replace(path, string.Empty);

        if (path.Contains("?"))
        {
            if (path.Contains("page="))
            {
                int index = path.IndexOf("page=");
                path = path.Substring(0, index);
            }
            else
            {
                path += "&";
            }
        }
        else
        {
            path += "?";
        }

        int page = GetPageIndex();
        string url = path + "page={0}";

        //if (page != 1)
        hlNext.HRef = string.Format(url, page);
        //else
        //hlNext.HRef = path.Replace("?", string.Empty);

        hlPrev.HRef = string.Format(url, page + 2);

        if (page == 0)
        {
            hlNext.Visible = false;
        }
        else
        {
            (Page as BlogBasePage).AddGenericLink("next", "Next page", hlNext.HRef);
            Page.Title += " - Page " + (page + 1);
        }

        if (hlPrev.Visible)
            (Page as BlogBasePage).AddGenericLink("prev", "Previous page", string.Format(url, page + 2));
    }

Sep 30, 2010 at 10:25 PM

Ha! Got it working! In addition to paging the comments I want to sort them from newest to oldest. Modifying the CompareTo() member in PostComment.cs took care of that. But now I have a new problem. When a new comment is added the appendComment javascript function appends the comment to the end of the comment list instead of the beginning.

Does anyone know how to append the new comment to the beginning?