How can I display text with carriage returns in a GridView in ASP.Net

荒凉一梦 提交于 2019-12-11 03:31:59

问题


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 />") %>'.



来源:https://stackoverflow.com/questions/9192469/how-can-i-display-text-with-carriage-returns-in-a-gridview-in-asp-net

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!