Click here to Skip to main content
15,892,809 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I have nested datalist. Parent datalist displays answers and child datalist displays comments for a particular answer. Suppose there are 2 answers and for 1st answer there are 2 comments "hi","Hello" and for 2nd answer there is 1 comment "Welcome". Then it should display like:
Example:
----------------------------
Answer 1
"hi"
"hello"

Answer 2
"Welcome"
-----------------------------
But it is displaying like:
-----------------------------
Answer 1
"hi"
"hello"
"hi"
"hello"

Answer 2
"Welcome"
"Welcome"
-----------------------------------------------------------------
Below is the code of ASPX and C#
------------------------------------------------------------------
ASPX:
-------------------------
ASP.NET
<asp:ScriptManager ID="ScriptManager1" runat="server"></asp:ScriptManager>
                       <asp:UpdatePanel ID="UpdatePanel1" runat="server">
                            <ContentTemplate>
                                <asp:DataList ID="dlAnswers" runat="server" RepeatColumns="1" RepeatDirection="Vertical" OnItemCommand="Datalist_Answer" DataKeyField="Aid" OnItemDataBound="dlAnswers_ItemDataBound" >
                               <ItemTemplate>
                                <div class="comment-body comment-body-answered clearfix">
                           <div class="avatar">
                               <asp:Image ID="ProfileImg" runat="server" ImageUrl='<%#Eval("ProfileImg") %>' />
                           </div>
                           <div class="comment-text">
                               <div class="author clearfix">

                                   <div class="comment-meta">
                                      <span>Author :<a href="#"> <%#Eval("FirstName")+" "+Eval("LastName") %></a></span>

                                       <div class="date"><i class="icon-time"></i>Posted on: <%#Eval("AnsDate") %></div>
                                       <a class="button small green-button" href="#" style="float:right;padding:5px 15px;margin-left:300px;text-align:center;width:130px;font-size:12px;margin-top:-20px">Accept Solution</a>
                                   </div>

                               </div>
                               <br />
                               <div class="clearfix"></div>
                               <div class="text">
                                   <asp:Label ID="lblAnswer" runat="server" Text='<%#Eval("Description")%>'></asp:Label>
                               </div>
                               <br />

                               <div id="AnsCommentBox" class="question-answered question-answered-done">
                                  <asp:DataList ID="dlAnswerComment" runat="server" RepeatDirection="Vertical">
                                      <ItemTemplate>
                                           <ul class="children">
                                           <li class="comment">
                                               <div class="comment-body clearfix">

                                               <div class="comment-text">
                                           <div class="author clearfix">
                                           <div class="comment-author"><a href="#"><%#Eval("FirstName")+" "+ Eval("LastName") %></a></div>
                                           <div class="comment-meta">
                                               <div class="date"><i class="icon-time"></i><%#Eval("AnsCDate") %></div>
                                           </div>

                                       </div>
                                       <div class="text">
                                           <p><asp:Label ID="lblAnsComment" runat="server" Text='<%#Eval("AnsComments")%>'></asp:Label></p>
                                       </div>
                                   </div>
                               </div>

                           </li>
                       </ul>

                       </ItemTemplate>
                       </asp:DataList>
                                  <asp:Button ID="btnComment" runat="server" Text="Have a Comment?" CssClass="button small blue-button" CommandName="GenerateCommentBox" />
                                   <asp:Panel ID="PlComment" runat="server" Visible="false" >
                                       <asp:TextBox ID="txtAnswerComment" runat="server" TextMode="MultiLine" CssClass="noresize"></asp:TextBox>
                                       <asp:Button ID="btnSumbit" runat="server" CssClass="button small blue-button" Text="Submit" Font-Size="12px" CommandName="SubmitComment" />
                                        <asp:Button ID="btnCancel" runat="server" CssClass="button small red-button" Text="Cancel" Font-Size="12px" CommandName="CancelButton"/>
                                   </asp:Panel>

                                  </div>
                           </div>
                       </div>

                          <hr />

                           </ItemTemplate>
                    </asp:DataList>
           </ContentTemplate>
         </asp:UpdatePanel>

----------------------------------------------------------------------
C#
---------------------------------------------------------------------
C#
protected void Page_Load(object sender, EventArgs e)
     {

         if (!IsPostBack)
         {
             LoadAnswers();
         }

     }

     private void LoadAnswers()
     {
         li.QuestionID = Convert.ToInt32(Request.QueryString["qid"]);

         dlSingleQuestion.DataSource = li.Get_Question_By_Id(li);
         dlSingleQuestion.DataBind();

         dlAnswers.DataSource = li.Get_All_Answers(li);
         dlAnswers.DataBind();
     }
     protected void dlAnswers_ItemDataBound(object sender, DataListItemEventArgs e)
     {
          
       DataList dl = (DataList)e.Item.FindControl("dlAnswerComment");
      li.AnswerID =          Convert.ToInt32(dlAnswers.DataKeys[e.Item.ItemIndex].ToString());
                dl.DataSource = li.Get_All_Answer_Comment(li);
                dl.DataBind();         
        }


What I have tried:

Shall I use repeater control or Datalist is OK...
Posted
Updated 15-Apr-16 20:24pm

1 solution

Please take some help from the given link below:

Nested DataList in ASP.NET[^]
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900