views:

40

answers:

3

Anyone know how to create a threaded / nested comment system? I would like to learn how to do this for my blog that I am working on. I simply can't find anything useful out there. Some one surely must know how its done.

A: 

If you're taking suggestions for the editor, how about using WMD: The Wysiwym Markdown Editor

WMD is a simple, lightweight HTML editor for blog comments, forum posts, and basic content management. You can add WMD to any textarea with one line of code. Add live preview with one line more. WMD works in nearly all modern browsers, and is now completely free to use.

As for the data model, you'll have to decide, amongst other considerations, how deep that comment nest would be.

public interface IComment
{
    public int ID;
    public string Body;
    public string OwnerID;
    public DateTime CreatedOn;
    ...        
}
p.campbell
A: 

Consider there are some issues that you'd have to navigate here. Spam, registration, scale, etc.

If you wanted to leverage some pre-built solution, consider:

p.campbell
A: 

It is hard to believe that after 8 years of asp.net, no one has bothered to write a simple article or tutorial on how something like threaded comments that are so commonly used on millions of blogs is done. Search engines yield nothing but garbage as usual. Then again, Google is a worthless piece of you know what.

This is why asp.net takes such a long time to learn. Not that it is brain surgery, only the fact that getting beyond the basic stuff present in books is impossible because the information does not exist.

I spent a whole day thinking about this and finally solved the problem using a single table for comments, two stored procedures handling comments and replies separately, a listview for displaying the indented comments exactly where they belong and another listview for the comment form. Best of all, there is no limit to the depth of threads and the comments are cached.

I my comments database table, I created a new column that stores the sorting value calculated in the stored procedures. For comments the sorting value is set equal to its own comment id value and for comment replies the value is set equal to the parent sorting id concatenated with it's own comment id separated by a dot. If a sorting id has no dots, it is a top level comment. One dot equals one level depth, two equals two level depth etc.

So, the only code I had to write was two simple stored procedures and a bit of C# to get the number of dots and assign the appropriate css value for indentation. I did not need multiple tables with foreign keys, parent-child id relationships, complicated code or any of the exotic recommendations commonly suggested by the few who actually bother to answer on forums.

Fast, efficient and works like a charm. Common sense rules!

Scott W.