Reputation: 3945
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;
}
At the 2 records are stored in 'ChecklistRecordEntry' I want to loop through them and bind it to the label 'comment' but then each time the loop goes around it rewrites over the previous one
<asp:Label ID="comment" runat="server" />
What can I do so every time it goes round the loop it adds a new label underneath the previous one (which will be displayed on screen)- instead of using the same label...thanks
Thanks for the reply everyone..all good help...bit of a problem trying to display each record inside a seperate row on a table though
<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>
Displays the results all inside one cell...any ideas?
Upvotes: 0
Views: 269
Reputation: 4903
I would recommend a repeater, it is pretty much straightforward:
<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>
Then bind the data to the repeater:
ChecklistRecordEntry =
(from CRE in db.Checklist_Record_Entries
where CRE.Check_List_Records_Id == checklistRecordId
select CRE).ToList();
rptComments.DataSource = ChecklistRecordEntry;
rptComments.DataBind();
Upvotes: 2
Reputation: 10541
The other answers are fine but I would suggest using String.Join
.
comment.Text = string.Join("<br />",
ChecklistRecordEntry.Select(a => a.Comment));
You also don't need to use ToList()
for this, since this will accept an IEnumerable
.
Upvotes: 1
Reputation: 7705
Why not just do this:
foreach (Checklist_Record_Entry CRE in ChecklistRecordEntry)
{
comment.Text += CRE.Comment + "<br/>";
}
Upvotes: 1
Reputation: 23341
I would rather use some temp string variable in code-behind, which will be updated by += in foreach, and then, after foreach is done assingn to comment.Text this temp variable
Upvotes: 1