2013-03-03 50 views
0
ChecklistRecordEntry = 
      (from CRE in db.Checklist_Record_Entries 
      where CRE.Check_List_Records_Id == checklistRecordId 
      select CRE).ToList(); 

     foreach (Checklist_Record_Entry CRE in ChecklistRecordEntry) 
     { 
      comment.Text = CRE.Comment; 
     } 

在2个记录存储在“ChecklistRecordEntry”在我想通过这些循环并将其绑定到标签“评论”但后来每次循环将围绕它改写较前一个结果传递标签

<asp:Label ID="comment" runat="server" /> 

我能在每次绕着环路它增加了前一个(将显示在屏幕上)下的新标签的时间这样做 - 而不是使用相同的标签.. .thanks


感谢您的答复everyone..all很好的帮助...一个问题有点想显示在表中单独的行内的每个记录虽然

<table class="detailstable FadeOutOnEdit"> 
       <tr> 
        <th style="width:200px;"></th>  
        <th style="width:200px;">Item</th>  
        <th style="width:200px;"></th>  
        <th style="width:200px;"></th>  
        <th style="width:200px;"><asp:Label ID="CommentHeader" Text="Comment" /></th>  
       </tr> 
       <tr> 
        <th></th> 
        <th></th> 
        <th></th> 
        <th></th> 
        <th style="width:200px;"><asp:Label ID="Comment" runat="server"/></th> 
       </tr> 
</table> 

显示结果所有内一个单元......任何想法?

回答

2

我会建议一个中继器,它是非常简单的:

<asp:Repeater id="rptComments" runat="server"> 
    <HeaderTemplate> 
     <table class="detailstable FadeOutOnEdit"> 
      <tr> 
       <th style="width:200px;"></th>  
       <th style="width:200px;">Item</th>  
       <th style="width:200px;"></th>  
       <th style="width:200px;"></th>  
       <th style="width:200px;"><asp:Label ID="CommentHeader" Text="Comment" /></th>  
      </tr> 
    </HeaderTemplate> 
    <ItemTemplate> 
     <tr> 
      <th></th> 
      <th></th> 
      <th></th> 
      <th></th> 
      <th style="width:200px;"><%# Eval("Comment") %></th> 
     </tr> 
    </ItemTemplate> 
    <FooterTemplate> 
     </table> 
    </FooterTemplate> 
</asp:Repeater> 

然后将数据绑定到中继器:

ChecklistRecordEntry = 
     (from CRE in db.Checklist_Record_Entries 
     where CRE.Check_List_Records_Id == checklistRecordId 
     select CRE).ToList(); 

rptComments.DataSource = ChecklistRecordEntry; 
rptComments.DataBind(); 
+0

感谢您的答复,怎么会我把它绑定在我的for循环中作为foreach(Checklist_Record_Entry CRE in ChecklistRecordEntry) { rptComments.DataBind = string.Join(“
”, ChecklistRecordEntry.Se lect(a => a.Comment)); } – John 2013-03-03 19:59:47

+0

@John,更新了适合表范例的答案。 – 2013-03-03 20:05:28

+0

非常好的工作非常感谢 – John 2013-03-03 20:08:11

1

我宁愿使用代码隐藏一些临时字符串变量,它会被更新+ =的foreach中,然后的foreach完成后assingn到comment.Text这个临时变量

1

为什么不只是做这个:

foreach (Checklist_Record_Entry CRE in ChecklistRecordEntry) 
{ 
    comment.Text += CRE.Comment + "<br/>"; 
} 
+0

啊不错的工作,感谢 – John 2013-03-03 20:09:01

1

其他答案都很好,但我会建议使用String.Join

comment.Text = string.Join("<br />", 
    ChecklistRecordEntry.Select(a => a.Comment)); 

你也不需要使用ToList()这一点,因为这将接受IEnumerable