Shahbaz Khan

Shahbaz Khan

  • NA
  • 129
  • 5.9k

Display nested replies for a comments in a blog?

Aug 8 2018 12:47 AM
I'm developing a blog that has a section for getting some comments and replies from the users. The first part is of my code for getting the comments and replies is the following one:
 
C# Code:
  1. var a = commentBLL.GetAll(BlogID); // Getting the list of comments in a blog  
  2. rp_MainComments.DataSource = a; // the outer repeater binding  
  3. rp_MainComments.DataBind();  
  4. -- this is the onitemdatabound function called from outer repeater --  
  5. protected void rp_MainComments_ItemDataBound(object sender, RepeaterItemEventArgs e)  
  6. {  
  7. if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)  
  8. {  
  9. LinkButton btn = e.Item.FindControl("btn_Comment_Reply"as LinkButton;  
  10. ScriptManager.GetCurrent(this).RegisterAsyncPostBackControl(btn);  
  11. HiddenField lt = (HiddenField)e.Item.FindControl("hf_Main_CommentID");  
  12. Repeater rpcommentreply = (Repeater)e.Item.FindControl("rp_Comment_Reply");  
  13. Panel panel_Comment_Reply = e.Item.FindControl("panel_Comment_Reply"as Panel;  
  14. if (!string.IsNullOrEmpty(lt.Value))  
  15. {  
  16. var b = commentBLL.GetByCommentID(Convert.ToInt32(lt.Value));  
  17. rpcommentreply.DataSource = b;  
  18. rpcommentreply.DataBind();  
  19. panel_Comment_Reply.Visible = false;  
  20. }  
  21. }  
  22. }  
aspx Code:
  1. <asp:Repeater ID="rp_MainComments" OnItemDataBound="rp_MainComments_ItemDataBound" OnItemCommand="rp_MainComments_ItemCommand" runat="server">  
  2. <ItemTemplate>  
  3. <asp:HiddenField ID="hf_Main_CommentID" runat="server" Value='<%# Eval("Comment_Id") %>' />  
  4. <ul class="comments">  
  5. <li>  
  6. <div class="comment">  
  7. <div class="img-thumbnail d-none d-sm-block">  
  8. <%--<asp:Image ID="Image_7" CssClass="avatar" runat="server" ImageUrl="http://placehold.it/40x40" />--%>  
  9. <i class="fa fa-user fa-fw fa-5x"></i>  
  10. </div>  
  11. <div class="comment-block">  
  12. <div class="comment-arrow"></div>  
  13. <span class="comment-by">  
  14. <strong><%# Eval("Name") %></strong>  
  15. <span class="float-right">  
  16. <span>  
  17. <asp:LinkButton ID="btn_Comment_Reply" Text="Reply" ClientIDMode="AutoID" runat="server" OnClick="btn_Comment_Reply_Click" CommandName="Edit" CommandArgument='<%# Eval("Comment_Id") %>' /></span>  
  18. </span>  
  19. </span>  
  20. <p><%# Eval("Message") %></p>  
  21. <span class="date float-right">  
  22. <%# Eval("Comment_Date") %></span>  
  23. </div>  
  24. </div>  
  25. </li>  
  26. <li>  
  27. <asp:HiddenField ID="hf_PanelValue" Value='<%# Container.ItemIndex %>' runat="server" />  
  28. <asp:Repeater ID="rp_Comment_Reply" OnItemCommand="rp_Comment_Reply_ItemCommand" runat="server">  
  29. <ItemTemplate>  
  30. <ul class="comments reply" id="commentreply">  
  31. <li>  
  32. <div class="comment">  
  33. <div class="img-thumbnail d-none d-sm-block">  
  34. <%--<asp:Image ID="Image_8" CssClass="avatar" runat="server" ImageUrl="http://placehold.it/40x40" />--%>  
  35. <i class="fa fa-user fa-5x fa-fw"></i>  
  36. </div>  
  37. <div class="comment-block">  
  38. <div class="comment-arrow"></div>  
  39. <span class="comment-by">  
  40. <strong><%# Eval("Name") %></strong>  
  41. <span class="float-right">  
  42. <span>  
  43. <asp:LinkButton ID="btn_Reply" Text="Reply" ClientIDMode="AutoID" runat="server" OnClick="btn_Reply_Click" CommandName="Edit" CommandArgument='<%# Eval("Parent_Id") %>' /></span></span>  
  44. </span>  
  45. <p><%# Eval("Message") %></p>  
  46. <span class="date float-right"><%# Eval("Comment_Date") %></span>  
  47. </div>  
  48. </div>  
  49. </li>  
  50. </ul>  
  51. </ItemTemplate>  
  52. </asp:Repeater>  
  53. </li>  
  54. </ul>  
  55. </ItemTemplate>  
  56. </asp:Repeater>  
Output:
 
Comment one
   reply one
   reply two
   reply three
comment two
   reply one
   reply two
 
But i need like this:
 
Comment one
   reply one
      reply one
      reply two
   reply three
      reply one
      reply two
comment two
   reply one
      reply one
   reply two
 
Now, I would like to show the nested replies as shown above
 
how is it possible?

Attachment: Untitled.zip

Answers (1)