1 sql asp.net threaded-comments c#-4.0
我正在开发ASP.net 4.0和sql server 2008中的博客,并想学习如何创建一个线程评论系统.通过线程我的意思是每个注释都有一个回复链接,注释在注释下缩进,它是一个回复.因此,您可以回复文章本身或回复任何评论.
这在论坛和博客上很常见,但我找不到任何可以解释和展示代码示例的文章.
以下是我创建的内容,但它仅适用于一个深度级别.我想让它递归,所以深度水平没有限制:我怎样才能做到这一点?任何建议,带有代码示例的文章都会很棒!
注释数据库表
commentId
parentId
postId
date
author
authorEmail
authorURL
authorIP
content
IsApproved
Run Code Online (Sandbox Code Playgroud)
ASP.NET标记:
<asp:ListView ID="ListView1" runat="server" onitemdatabound="ListView1_ItemDataBound">
<ItemTemplate>
<div class="commentwrap">
<div class="commentsTitleArea">
<span class="commentCounter"><%# Convert.ToInt32(Container.DisplayIndex) + 1%>. </span> <img src="../images/decoy-icon-16px.png" alt="Comment by..." title="Comment by..." class="blogCommentIcon" /><a href='<%# Eval("AuthorUrl")%>' target="_blank" rel="nofollow"><%# " " + Eval("Author")%></a> <%# Eval("Date")%></div>
<div class="commentText">
<%# Eval("Content") %>
<div><span class="btnCommentReply"><a href='<%# "article.aspx?article=" + Request.QueryString["article"] + "&cid=" + Eval("commentId") + "#comment" %>'>REPLY</a></span></div>
</div>
<asp:ListView ID="ListView2" runat="server">
<ItemTemplate>
<div class="commentwrap commentNest">
<div class="commentsTitleArea">
<span class="commentCounter"><%# Convert.ToInt32(Container.DisplayIndex) + 1%>. </span> <img src="../images/decoy-icon-16px.png" alt="Comment by..." title="Comment by..." class="blogCommentIcon" /><a href='<%# Eval("AuthorUrl")%>' target="_blank" rel="nofollow"><%# " " + Eval("Author")%></a> <%# Eval("Date")%></div>
<div class="commentText">
<%# Eval("Content") %>
</div>
</div>
</ItemTemplate>
<EmptyDataTemplate>
</EmptyDataTemplate>
<LayoutTemplate>
<div id="itemPlaceholderContainer" runat="server">
<span id="itemPlaceholder" runat="server" />
</div>
</LayoutTemplate>
</asp:ListView>
</div>
</ItemTemplate>
<EmptyDataTemplate>
</EmptyDataTemplate>
<LayoutTemplate>
<div id="itemPlaceholderContainer" runat="server">
<span id="itemPlaceholder" runat="server" />
</div>
<div class="dataPagerWrap">
<asp:DataPager ID="ListViewpager" runat="server" PagedControlID="ListView1" PageSize="30" QueryStringField="page">
<Fields>
<asp:NextPreviousPagerField ShowFirstPageButton="True" ShowNextPageButton="False" ShowPreviousPageButton="False" FirstPageText="«" ButtonCssClass="dataPagerBackForward" />
<asp:NumericPagerField ButtonCount="8" CurrentPageLabelCssClass="dataPagerCurrent" NumericButtonCssClass="dataPager" PreviousPageText="..." NextPageText="..." NextPreviousButtonCssClass="dataPagerBackForward" />
<asp:NextPreviousPagerField ShowLastPageButton="True" ShowNextPageButton="False" ShowPreviousPageButton="False" LastPageText="»" ButtonCssClass="dataPagerBackForward" />
</Fields>
</asp:DataPager>
</div>
<div class="padding"></div>
</LayoutTemplate>
Run Code Online (Sandbox Code Playgroud)
ALTER PROCEDURE [dbo].[sp_blog_GetComments]
(
@article int
)
AS
SET NOCOUNT ON;
SELECT
post_Comments.Author,
post_Comments.AuthorEmail,
post_Comments.AuthorUrl,
post_Comments.Content,
post_Comments.Date,
post_Comments.commentId
FROM post_Comments
INNER JOIN
posts
ON post_Comments.postId = posts.postId
WHERE(post_Comments.postId = @article)
AND (post_Comments.IsApproved = 1)
AND (post_Comments.ParentId IS NULL)
AND (posts.IsPublished = 1)
AND (posts.PublishOnDate <= GETDATE())
SELECT
Author,
AuthorEmail,
AuthorUrl,
Content,
Date,
ParentId
FROM post_Comments
WHERE (postId = @article)
AND (IsApproved = 1)
Run Code Online (Sandbox Code Playgroud)
存储过程:
ALTER PROCEDURE [dbo].[Sp_blog_getcomments] (@article INT)
AS
SET nocount ON;
SELECT post_comments.author,
post_comments.authoremail,
post_comments.authorurl,
post_comments.content,
post_comments.date,
post_comments.commentid
FROM post_comments
INNER JOIN posts
ON post_comments.postid = posts.postid
WHERE ( post_comments.postid = @article )
AND ( post_comments.isapproved = 1 )
AND ( post_comments.parentid IS NULL )
AND ( posts.ispublished = 1 )
AND ( posts.publishondate <= Getdate() )
SELECT author,
authoremail,
authorurl,
content,
date,
parentid
FROM post_comments
WHERE ( postid = @article )
AND ( isapproved = 1 )
Run Code Online (Sandbox Code Playgroud)
我记得在某个地方看到的一种方法不是使用注释ID和父ID,而是注释具有注释ID和"排序键",它是其祖先的所有注释ID的串联.
例如,如果注释1有两个回复,注释2和3,则排序键将是:
1 : 0001
2 : 0001.0002
3 : 0001.0003
Run Code Online (Sandbox Code Playgroud)
然后,如果有人回复评论2,那将是..
4 : 0001.0002.0004
Run Code Online (Sandbox Code Playgroud)
因此,如果您选择所有注释并按此排序键排序,它们将以正确的顺序排除.
然后,要进行缩进,只需查看排序键的长度即可查看注释的深度级别,并缩进适当的数量.
添加注释很简单:新注释的排序键只是它的父级排序键,并在末尾添加了自己的ID.
| 归档时间: |
|
| 查看次数: |
2274 次 |
| 最近记录: |