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' 中,我想遍历它们并将其绑定到标签 'comment' 但每次循环绕过它都会重写前一个

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

每次循环时我能做什么,它会在前一个标签下方添加一个新标签(将显示在屏幕上) - 而不是使用相同的标签......谢谢


感谢大家的回复..所有很好的帮助...虽然尝试在表格的单独行中显示每条记录有点问题

<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>

在一个单元格中显示所有结果...有什么想法吗?

4

4 回答 4

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();
于 2013-03-03T19:51:28.713 回答
1

我宁愿在代码隐藏中使用一些临时字符串变量,这将在 foreach 中由 += 更新,然后在 foreach 完成后分配给comment.Text 这个临时变量

于 2013-03-03T19:39:14.233 回答
1

为什么不这样做:

foreach (Checklist_Record_Entry CRE in ChecklistRecordEntry)
{
    comment.Text += CRE.Comment + "<br/>";
}
于 2013-03-03T19:40:37.647 回答
1

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

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

您也不需要为此使用ToList(),因为这将接受IEnumerable.

于 2013-03-03T19:43:07.557 回答