Display label text with line breaks in c#

后端 未结 8 569
面向向阳花
面向向阳花 2020-12-05 09:28

Is it possible to display the label text with line breaks exactly as per the image

\"enter

相关标签:
8条回答
  • 2020-12-05 09:56

    You can also use <br/> where you want to break the text.

    0 讨论(0)
  • 2020-12-05 10:03

    Following line worked for me:

    lbTabRes.Text += num + " x " + i + " = " + (num * i).ToString() + "<br/> \n";
    
    0 讨论(0)
  • 2020-12-05 10:05

    I had to replace new lines with br

    string newString = oldString.Replace("\n", "<br />");
    

    or if you use xml

    <asp:Label ID="Label1" runat="server" Text='<%# ShowLineBreaks(Eval("Comments")) %>'></asp:Label>
    

    Then in code behind

    public string ShowLineBreaks(object text)
    {
        return (text.ToString().Replace("\n", "<br/>"));
    }
    
    0 讨论(0)
  • 2020-12-05 10:06

    Or simply add one line of:

    Text='<%# Eval("Comments").ToString().Replace("\n","<br />") %>'
    
    0 讨论(0)
  • 2020-12-05 10:09

    Also you can use the following

    @"Italian naval...<br><br>"+
    

    Above code you can achieve double space. If you want single one means you simply use
    .

    0 讨论(0)
  • 2020-12-05 10:15

    I know this thread is old, but...

    If you're using html encoding (like AntiXSS), the previous answers will not work. The break tags will be rendered as text, rather than applying a carriage return. You can wrap your asp label in a pre tag, and it will display with whatever line breaks are set from the code behind.

    Example:

    <pre style="width:600px;white-space:pre-wrap;"><asp:Label ID="lblMessage" Runat="server" visible ="true"/></pre>
    
    0 讨论(0)
提交回复
热议问题