The secret newline symbol for html encoded controls

ASCII Character 10 is an under loved character in the ASCII set. It lurks around there right at the beginning in the non-displayed range and most of the time you use it you don't even realise it.

Well you probably already guessed from the subject of this post that this mysterious symbol is actually the newline character.

A lot of controls automatically html encode their output, and rightly so, its asp.net's way of protecting you from cross site scripting attacks and it also makes your site much more likely to show you a green light when you post it through the w3c validator.

But sometimes you want a specific markup to be displayed. Whether its you or your boss that's the perfectionist you need that newline and your <br /> or \n have failed you.

So how do you encode the newline? ASCII 10 to the rescue!

The entity looks like this:

&#010;

and it can be used in your html encoded strings because its already html encoded!

I actually found this tip tucked away in a thread on the asp.net forums (see further reading). It was half way down the thread and it didn't even get marked as the answer! Credit where credits due - thanks for this tip Mohamed Alsakaf!

Usage Example

<asp:Button ID="Button1" runat="server" Text="An example of a&#010;long button&#010;with complete control over the newlines" />

Further Reading

kick it on DotNetKicks.com Shout it vote it on WebDevVote.com

2 comments :

Anonymous said...

awesome

Anonymous said...

Genius - thank you, thank you, thank you!

I have been looking for a way to format screentips in visio documents that have been published as HTML and this has given me the answer