【问题标题】:asp:label does not respect line breaks read in from text file. Line wrap is not as intendedasp:label 不尊重从文本文件中读取的换行符。换行不符合预期
【发布时间】:2015-02-16 21:10:20
【问题描述】:

我有一个 txt 文件,我尝试使用 asp:label 在 aspx 页面上显示该文件。

我希望标签尊重 .txt 文件的结构,但文本显示在一个大包装中,尽管页面源表明它已正确加载。

另一种方法是使用多行文本框,但这让我“感觉”好像我错过了一些简单的标签。

如何让标签尊重 .txt 中的换行符?

.cs:

        StreamReader sr = new StreamReader(Server.MapPath(@"~/ReleaseNotes.txt"));
        lblReleaseNotes.Text = sr.ReadToEnd();
        StreamReader sr1 = new StreamReader(Server.MapPath(@"~/ReleaseNotes.txt"));
        txtReleaseNotes.Text = sr1.ReadToEnd();

.apsx:

<asp:Label ID="lblReleaseNotes" runat="server" Text="[unable to read ReleaseNotes]" Width="100%" Style="word-wrap: normal; word-break: break-all;" ></asp:Label>
<asp:TextBox ID="txtReleaseNotes" runat="server" Text="[unable to read ReleaseNotes]" ReadOnly="True" TextMode="MultiLine" Width="100%" Height="600" BorderStyle="None" BorderWidth="0"></asp:TextBox>

查看源代码:

<span id="MainContent_lblReleaseNotes" style="display:inline-block;width:100%;word-wrap: normal; word-break: break-all;">SnowKing Release Notes

0.3 - 13/02/2015 - Add Live Google Maps and Run Catalogue

- Generate Release Notes .txt
- Added single column runs catalogue to Fernie page.
- Added page for Val Thorens.
- Added page for Les Meniures.
...(etc)
</span>

【问题讨论】:

    标签: c# asp.net


    【解决方案1】:

    您正在使用标签将文本直接输出到 HTML。

    将换行符更改为&lt;br /&gt;,您就可以了:

    lblReleaseNotes.Text = sr.ReadToEnd().Replace("\n", "<br />");
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-09-23
      • 2021-12-09
      • 1970-01-01
      • 1970-01-01
      • 2018-08-22
      • 1970-01-01
      相关资源
      最近更新 更多