How can I display text with carriage returns in a

2019-07-14 06:15发布

问题:

I'm trying to display records within my GridView and a few of the cells contain text with carriage returns. However when this text is displayed the carriage returns are gone. Is there an easy way to display carriage returns in a GridView Cell?

I've tried using a TextBox within the cell which works but I then have the scollbars of the TextBox. I'm not too keen on this way either as the textbox may render differently in different browser.

My code is displayed below, I've left my testing in there so you can see all the ways I've tried:

<asp:GridView ID="gridResults" runat="server"  AutoGenerateColumns="false" CellSpacing="-1" 
            CssClass ="stretch separate" GridLines="None" EnableViewState="true">
            <HeaderStyle CssClass="header pad" />
            <RowStyle CssClass="row pad"/>                
            <EmptyDataRowStyle CssClass="empty pad" />
            <Columns>
                <asp:BoundField DataField="Notes" HeaderText="Notes"  HtmlEncode="true">
                    <ItemStyle Width="100"   />
                </asp:BoundField>
                <asp:TemplateField HeaderText="Notes" >
                    <ItemTemplate>
                        <asp:Label ID="test" runat="server"   Text='<%# Bind("Notes") %>' />
                        <asp:TextBox ID="textNotes" runat="server" CssClass="right"  TextMode="MultiLine" Text='<%# Bind("Notes") %>' BorderStyle="None" />
                    </ItemTemplate>                        
                    <ItemStyle Width="50" CssClass="right"  />
                </asp:TemplateField>
            </Columns>
        </asp:GridView>

I'm using ASP.Net 4.0 and c#

Thanks

回答1:

Try setting the Text attribute of the <asp:Label> to Text='<%# Eval("Notes").ToString().Replace("\n", "<br />") %>'.