本文介绍了在ASP.NET中绑定嵌套的datalist的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述 29岁程序员,3月因学历无情被辞! 我有嵌套的datalist。父级datalist显示答案,子级datalist显示特定答案的注释。假设有2个答案,第1个答案有2条评论hi,Hello,第2个答案有1条评论欢迎。然后它应该显示如下: 示例: -------------------------- - 答案1 hi 你好 答案2 欢迎 ----------------------------- 但它显示如下: ---------------------------- - 答案1 hi 你好 hi 你好 答案2 欢迎 欢迎 ----------------------------------------------- ------------------ 下面是ASPX的代码和C# -------- -------------------------------------------------- -------- ASPX: ------------------------ - < asp:ScriptManager ID = ScriptManager1 runat = server > < / asp:ScriptManager > < asp:UpdatePanel ID = UpdatePanel1 runat = 服务器 > < ContentTemplate > < asp:DataList ID = dlAnswers runat = server RepeatColumns = 1 RepeatDirection = 垂直 OnItemCommand = Datalist_Answer DataKeyField = 援助 OnItemDataBound = dlAnswers_ItemDataBound > < ItemTemplate > < div class = comment-body comment-body-answers clearfix > < div class = 头像 > < asp:Image ID = ProfileImg runat = server ImageUrl =' <% #Eval( ProfileImg)%>' / > < / div > < div class = comment-text > < div class = 作者clearfix > < div 类 = comment-meta > < span > 作者:< a href = # > <% #Eval( FirstName)+ + Eval( LastName)%> < / a > < / span > < div class = date > < i class = icon-time > < / i > 发​​布于:<% #Eval( AnsDate)%> < ; / div > < a class = 按钮小绿色按钮 href = # style = float:right; padding:5px 15px; margin-left:300px; text-align:center; width:130px; font-size:12px; margin-top:-20px > 接受解决方案< / a > < / div > < / div > < br / > < div class = clearfix > < / div > < div class = text > < asp:Label ID = lblAnswer runat = server 文本 =' <% #Eval( 描述)%>' > < / asp:Label > < / div > < br / > < div id = AnsCommentBox class = 问题解答的问题-answered-DONE > < asp:DataList ID = dlAnswerComment runat = 服务器 RepeatDirection = 垂直 > < ItemTemplate > < ul class = 儿童 > < li class = comment > < div class = comment-body clearfix > < div class = 评论文字 > < div class = 作者clearfix > < div class = comment-author > < a hre f = # > <% #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 = lblAns注释 runat = server 文本 =' <% #Eval( AnsComments)%>' > < / asp:标签 > < / p > < / div > < / div > < / div > < / li > < / ul > < / ItemTemplate > < / asp:DataList > < asp:Button ID = btnComment runat = server 文本 = 有评论吗? CssClass = 按钮小蓝色按钮 CommandName = GenerateCommentBox / > < asp:Panel ID = PlComment runat = server 可见 = false > < asp:TextBox ID = txtAnswerComment runat = server TextMode = MultiLine CssClass = noresize > < / asp:TextBox > < asp:按钮 ID = btnSumbit runat = server CssClass = button small blue-button 文本 = 提交 字体大小 = 12px CommandName = SubmitComment / > < asp:按钮 ID = btnCa ncel runat = server CssClass = 按钮小红色按钮 文字 = 取消 字体大小 = 12px CommandName = CancelButton / > < / asp:Panel > < / div > < / div > < ; / div > < hr / > < / ItemTemplate > < / asp:DataList > < / ContentTemplate > < / asp:UpdatePanel > ------------------------------------------------- --------------------- 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(); } 我的尝试: 我应该使用转发器控制还是Datalist可以...... 解决方案 请从以下给定链接中获取一些帮助: ASP.NET中的嵌套数据列表 [ ^ ] 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: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#---------------------------------------------------------------------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... 解决方案 Please take some help from the given link below:Nested DataList in ASP.NET[^] 这篇关于在ASP.NET中绑定嵌套的datalist的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持! 上岸,阿里云!
09-05 22:58