embedding CRLF

J

John Grandy

I am not having success embedding carriage return and line feed characters
into a string and displaying them with an ASP.NET label. Is anyone getting
either of the following to work ?

StringBuilder message = new StringBuilder();
message.Append("Line 1");
message.Append("\r\n");
message.Append("Line 2");
Label.Text = message.ToString();

StringBuilder message = new StringBuilder();
message.Append("Line 1");
message.AppendLine();
message.Append("Line 2");
Label.Text = message.ToString();
 
J

John Grandy

Yeah, wrong forum, whatever.

There's actually a decent reason why I thought \r\n would work.

The ASP.NET Label control performs magic behind the scenes, such as HTML
encoding, to ensure that content shows up right.

However, directly embedding <br/> doesn't work , because ASP.NET renders the
Label control as a <span> with literal content

&lt;br/&gt;

which will show on the web page as

<br/>


And of course performing the HTML encoding yourself makes matters even worse
:

Server.HMTLEncode("<br/>") is rendered as

amp;lt;br/amp;gt;

I was thinking the ASP.NET Label control would be smart enough to recognize
\r\n and render it as <br/> ... nope. I don't think an ASP.NET Label
control is capable of rendering the line breaks.

However, you can use a server-side span element ...

..aspx
<span id="span1" runat="server">

..aspx.cs
HtmlGenericControl span1;
span1.InnerHtml = "blah blah blah <br/> blah blah blah";
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top