我有嵌套的数据列表。父数据列表显示答案,子数据列表显示特定答案的注释。假设有2个答案,第1个答案有2个评论" hi"," Hello"对于第二个答案,有1条评论"欢迎"。然后它应显示如下:
答案1
喜
你好
答案2
欢迎
答案1
喜
你好
喜
你好
答案2
欢迎
欢迎
<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>
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();
}
答案 0 :(得分:0)
我解决了。上面的代码是正确的,问题出在SELECT查询中。因为我没有匹配用户的注册ID。这样做后,记录显示正确。