Is it possible to display the label text with line breaks exactly as per the image
I need the text on the label to be printed exactly can some one help me, my desired text that has to be shown on the label will be stored in a string-builder
You can wrap your asp label in a pre tag, and it will display with whatever line breaks are set from the code behind. Show activity on this post. You can also use <br/> where you want to break the text.
To add line breaks in the text we'll use \n character in the string we want to assign to the label.
Basic HTML Line Break Syntax You can insert line breaks in HTML with the <br> tag, which is equivalent to a carriage return on a keyboard.
Line breaks in HTML or ASP.NET is represented by the < br /> tag. In some cases, you will need to convert an existing string that contains new lines to a valid HTML code that includes < br /> tags.
You may append HTML <br />
in between your lines. Something like:
MyLabel.Text = "SomeText asdfa asd fas df asdf" + "<br />" + "Some more text";
With StringBuilder you can try:
StringBuilder sb = new StringBuilder(); sb.AppendLine("Some text with line one"); sb.AppendLine("Some mpre text with line two"); MyLabel.Text = sb.ToString().Replace(Environment.NewLine, "<br />");
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>
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With