Geeks With Blogs
Steal This Code All good programmers borrow at least part of their code. So feel free to borrow some of mine.
First off, sorry for the absence. Things got a little busy in life as well as work. Not to mention having to get up to speed on 2.0 in a big hurry. All good things, but it left less time for fun stuff.

With that said, I want to offer a little tip to help with formatting your ASP.Net pages. Getting a label to be multiline within a set width. Now many of you know this trick, but I always have to re-remember the trick every time I need it, so this post can be a reference for you as well.

The trick is actually this - Don't use a label. Use a textbox instead. Since all the controls inherit from the same base class, the text box and the label are very similar anyway. But only the textbox has the multiline capability.

Once you have the textbox on your page the trick is to make it look like a label for the end user. To do this you need to set the following properties (This is the part I can never remember):

Wrap = true; (obviously)
rows = {some number greater than 0} (I use the rows property rather than the height property as it tends to be earier to get the formatting right)
ReadOnly = true (makes sense, right)
TextMode = multiline (or there no real reason to wrap the text...)
BorderStyle = None
BorderWidth = 0

Those last two are actually VERY important, and here is why. They get rid of that pesky border that you see around text boxes. That border is a common visual signal to the end user that says "ENTER INFO HERE!". If you leave the border up expect a lot of phone calls from folks saying "The web page is broken. It won't let me enter text."

So use this in good faith. More controls soon. Even my first 2.0 controls. Posted on Tuesday, March 21, 2006 6:04 AM Tips Tricks and Frustrating Errors | Back to top


Comments on this post: How to make your ASP.Net label multiline (how to wrap text in your label)

# re: How to make your ASP.Net label multiline (how to wrap text in your label)
Requesting Gravatar...
Nice suggestion, but dont u think that a Disabled Textbos looks different from a label, in the sense that for eg. in my application, i need multiline labels with font VERDANA and wight BOLD. so for a disabled-textbox, i guess am unable to set these properties for the displayed text.
if u cangive any suggestions on this, thn my problm is solved !!

thnx,
Gaurav Aggarwal
gauravaggarwal.cs@gmail.com
TCS Kolkata
Left by gaurav aggarwal on Apr 15, 2006 2:23 AM

# re: How to make your ASP.Net label multiline (how to wrap text in your label)
Requesting Gravatar...
So how do you get rid of the scroll bar, which spoils the whole effect?
Left by Nick Chivers on May 08, 2006 6:49 PM

# re: How to make your ASP.Net label multiline (how to wrap text in your label)
Requesting Gravatar...
You don't need to do anything to the label. Just concatinate your text like the following:

"some text" & "<BR>" & "text in the second line"....

<BR> puts the new line break.

Left by agnieszka on Jul 13, 2006 8:56 AM

# re: How to make your ASP.Net label multiline (how to wrap text in your label)
Requesting Gravatar...
How about just using the css white-space style...
Left by Justin Wignall on Jul 16, 2006 1:47 PM

# re: How to make your ASP.Net label multiline (how to wrap text in your label)
Requesting Gravatar...
when i using a form and i save all the content of the textbox with multiline in a database, then i want to see the same format text as the textbox without using a textbox, like php or asp 1 does.
Left by Arthur on Sep 27, 2006 3:47 PM

# re: How to make your ASP.Net label multiline (how to wrap text in your label)
Requesting Gravatar...
To remove the scrollbars you can add

style="overflow:hidden"

to the textbox.

Left by Jazzwhistle on Sep 30, 2006 6:53 PM

# re: How to make your ASP.Net label multiline (how to wrap text in your label)
Requesting Gravatar...
A better way to make a label multiline (IMNTBHO) is do this:

lblUserExists.Text = "That user does not exist" + "\n\" +
"Please contact Security at xxx-xxxx if you have questions."

This is actual code, and it works just dandy.

Logan
Left by Logan Scott on Jan 02, 2007 2:12 PM

# re: How to make your ASP.Net label multiline (how to wrap text in your label)
Requesting Gravatar...
My problem is that I am putting the data in the asp:label in the codebehind.. Concatenating <BR> <br/> vbcrlf etc is not working..
Can anyone help me with this.
Left by Parag on Jan 05, 2007 1:49 PM

# re: How to make your ASP.Net label multiline (how to wrap text in your label)
Requesting Gravatar...
The issue is quite often relevant when you feed multiline data from a database. Mostly the data in the database will use character codes 13 followed by a code 10 for new line.

I uses the pre-render event of the label to change the newline characters to <br/> statements like this :

Protected Sub Label_PreRender(ByVal sender As Object, ByVal e As System.EventArgs)
Dim lbl As Label = sender
lbl.Text = Replace(lbl.Text, Chr(13) + Chr(10), "<br/>")
End Sub

//Per
Left by Per on Jan 05, 2007 10:15 PM

# re: How to make your ASP.Net label multiline (how to wrap text in your label)
Requesting Gravatar...
How About using
"Some text " + Envronment.newLine + "Some text"
Left by shomun on Jan 23, 2007 6:01 AM

# re: How to make your ASP.Net label multiline (how to wrap text in your label)
Requesting Gravatar...
Unless you are providing a "title+body" message like Logan Scott (and that can be managed by using 2 seperate labels), all these comments about inserting a newline at a hardcoded location in the string assume that ALL your users have the same screen resolution as you.

That's a very bad assumption, unless you're trying to propogate the "arrogant developer" stereotype. No offense intended, of course. :)
Left by Dan on Jan 31, 2007 1:01 PM

# re: How to make your ASP.Net label multiline (how to wrap text in your label)
Requesting Gravatar...
Hi guys

It took me quite some time to figure out how to wrap the text in your label. The point here is not to make the label to wrap the text but configure the text a little bit so that it can wrap by it self. Here is how.

This is the function to make the text wrappable:

public static string WrappableText(string source)
{
string nwln = Environment.NewLine;
return "<p>" +
source.Replace(nwln + nwln , "</p><p>")
.Replace(nwln, "<br />") + "</p>";
}

This is how to use it
OutputLabel.Text = WrappableText("[put your text here]");

The code is written in C#. Hope this will help someone out there.
Left by Long on Feb 04, 2007 7:59 PM

# re: How to make your ASP.Net label multiline (how to wrap text in your label)
Requesting Gravatar...
It worked great! very smart way!
Left by Daryoush on Feb 06, 2007 6:48 PM

# re: How to make your ASP.Net label multiline (how to wrap text in your label)
Requesting Gravatar...
hi,
im storing data in the label dynamically,so cant use <br> tags.is thr ne other way to wrap multiline text in a label dynamically.
i tried using a textbox.but the scrollbar is visible.how to make the scroll bar hidden???
Left by shruti on Feb 15, 2007 11:56 PM

# re: How to make your ASP.Net label multiline (how to wrap text in your label)
Requesting Gravatar...
Hi Long

Your solution worked a treat! Here is the converted vb.net code:-

Public Shared Function WrappableText(ByVal source As String) As String
Dim nwln As String = Environment.NewLine
Return "<p>" + source.Replace(nwln + nwln, "</p><p>").Replace(nwln, "<br />") + "</p>"
End Function

'----------------------------------------------------------------
' Converted from C# to VB .NET using CSharpToVBConverter(1.2).
' Developed by: Kamal Patel (http://www.KamalPatel.net)
'----------------------------------------------------------------

I saved this in a page utilities class and called it using:

label.text = "whatever multline " & string_variable & vbCrLf & " of concatentated text inclusive of vbCrLf "

label.text = utilities.pageutilities.WrappableText(label.text)
Left by mARK on Feb 19, 2007 10:49 AM

# re: How to make your ASP.Net label multiline (how to wrap text in your label)
Requesting Gravatar...
Strange that some people say the label method with <BR> / </br> etc works...

I have a master page and create a new page from it. If I then add a label and set the text to "line1</br>line2" the text is all written on the first line.

If I create a new page without basing it on the master page it works!

There seems to be some bug with this when using master pages..

Grrr....!

Left by JP on Feb 20, 2007 3:36 PM

# re: How to make your ASP.Net label multiline (how to wrap text in your label)
Requesting Gravatar...
I'm just wandering why don't you use asp:Panel and set wrapping to true, and just put the label inside the panel? It will do the trick just fine.
Left by Mihailo on Feb 27, 2007 7:01 AM

# re: How to make your ASP.Net label multiline (how to wrap text in your label)
Requesting Gravatar...
Instead of newline \n, I used <br> html tag to get multiline. This worked just fine -
lblWelcome.Text= "Welcome to <br> XYZ Global Organization's <br> Field Services Group" ;
Left by Mark K on Feb 27, 2007 2:12 PM

# re: How to make your ASP.Net label multiline (how to wrap text in your label)
Requesting Gravatar...
To make textbox resizable with no scrollbar I did this

protected void Page_Load(object sender, EventArgs e)
{
string message = getText();//get your text data from database

int numText = message.Length;

this.txtContent.Rows = numText / 50; //set textbox rows

this.txtContent.Text = message;

this.txtContent.Attributes.Add("style", "overflow :hidden");//hide textbox scrollbar
}

It works fine!!

Left by Apichart on Mar 05, 2007 2:27 AM

# re: How to make your ASP.Net label multiline (how to wrap text in your label)
Requesting Gravatar...
<a href="">Hello</a>
Left by dffhj on Mar 15, 2007 5:55 PM

# re: How to make your ASP.Net label multiline (how to wrap text in your label)
Requesting Gravatar...
Hey Long,
Thanks for the code.
I've used it for a databound datalist.
I added this code inside databound event of datalist to make the messageLabel multiline.

if (e.Item.ItemIndex != -1)
{
Label msgLabel = (Label)e.Item.FindControl("messageLabel");
msgLabel.Text = WrappableText(msgLabel.Text);
}

it works :)
Left by myherdem on Mar 16, 2007 8:32 AM

# re: How to make your ASP.Net label multiline (how to wrap text in your label)
Requesting Gravatar...
Am I using this incorrectly or does it not handle a word that is longer than the width of the label for example if someone is entering some kind of spam message with no spaces?
Left by James on Mar 25, 2007 12:49 PM

# Thank you - It was a big help
Requesting Gravatar...
This suggestion was a big help.....broke my head trying to use label control as a multiline textbox control.....

thank you - who ever wrote this article ...
Left by Prashant on Apr 20, 2007 1:33 PM

# re: How to make your ASP.Net label multiline (how to wrap text in your label)
Requesting Gravatar...
this is good suggestion but i dont want it i want it into label only.
Left by shb on Apr 24, 2007 4:37 AM

# re: How to make your ASP.Net label multiline (how to wrap text in your label)
Requesting Gravatar...
the code is awesome..it works great. I was trying to use \r\n to get a new line inserted in my text..that doesn't work for some reason.
but this method (WrappableText) is just cool. thanks to the author.
Left by Aftab. on May 17, 2007 6:21 AM

# re: How to make your ASP.Net label multiline (how to wrap text in your label)
Requesting Gravatar...
Easier solution that I've used in a formview and gridview control.

When in edit mode, you simply use a multi-line textbox. The line breaks are sent with the text to the database.

When you are in Read-Only mode, the text is taken from the database and pushed into a Label control. The Label control spits out HTML, which doesn't recognise the line breaks, leaving you with a jumbled mess.

So all you do is replace the line breaks (chr 13) with <br /> tags, like this:

<asp:Label ID="lblNotes" runat="server" Text='<%# Eval("Notes").ToString.Replace(chr(13), "<br />") %>' />

Works for me, hope it helps!
Left by JP on Jun 25, 2007 7:18 PM

# re: How to make your ASP.Net label multiline (how to wrap text in your label)
Requesting Gravatar...
this worked for me:

<asp:Label ID="lblItemContent" runat="server" Text='<%# Eval("Content").ToString().Replace("\n", "<br />") %>' ></asp:Label>
Left by asaf on Aug 12, 2007 3:19 AM

# re: How to make your ASP.Net label multiline (how to wrap text in your label)
Requesting Gravatar...
You guys missed it ---> Mihailo had your answer back on 2/27/2007 (read the posts!)

I'm just wandering why don't you use asp:Panel and set wrapping to true, and just put the label inside the panel? It will do the trick just fine.
2/27/2007 7:01 AM | Mihailo
Left by greg on Aug 30, 2007 4:29 PM

# re: How to make your ASP.Net label multiline (how to wrap text in your label)
Requesting Gravatar...
Hi,
this is a nice idea which is much simple, and the question is how to remove the vertical scroll bar.
When i execvute my application i can the see the scroll bar. This should not happen..
So please give me a suggestion...
Left by Sridhar on Sep 03, 2007 12:03 AM

# re: How to make your ASP.Net label multiline (how to wrap text in your label)
Requesting Gravatar...
Hello,
use this rule for the cssclass of the label:
white-space: normal;
It works for me.
Left by Paolo on Sep 06, 2007 4:06 AM

# re: How to make your ASP.Net label multiline (how to wrap text in your label)
Requesting Gravatar...
Hi All, Any idea how to wrap text for below text?
<TD>
<asp:Label ID="comments" Runat="server" text='<%# DataBinder.Eval(Container, "DataItem") ("my_comments")%>'></asp:Label>&nbsp;</TD>
Left by Dina on Sep 21, 2007 6:03 AM

# re: How to make your ASP.Net label multiline (how to wrap text in your label)
Requesting Gravatar...
<asp:Label Text='<%# Eval("Comment")==null ? Eval("Comment") : Eval("Comment").ToString().Replace("\n", "<br />") %>' runat="server" />
Left by Bob on Oct 23, 2007 1:06 PM

# re: How to make your ASP.Net label multiline (how to wrap text in your label)
Requesting Gravatar...
The <br> idea in the label field's Text property works great. Thanks.
Left by Scott Marlowe on Oct 24, 2007 8:00 AM

# re: How to make your ASP.Net label multiline (how to wrap text in your label)
Requesting Gravatar...
This is really wonderful. Thanks. I had a problem with the scrollbars but "overflow: hidden" did the trick.
Left by Noran on Oct 26, 2007 6:27 AM

# re: How to make your ASP.Net label multiline (how to wrap text in your label)
Requesting Gravatar...
that work fine with me
protected void Label_News_Content_PreRender(object sender, EventArgs e)
{
Label Label_News_Content = (Label)sender;
string f = Label_News_Content.Text;
Label_News_Content.Text = Label_News_Content.Text.Replace("\n", "<BR>");

}
Left by mohamed antar on Nov 05, 2007 11:57 AM

# re: How to make your ASP.Net label multiline (how to wrap text in your label)
Requesting Gravatar...
Hey guys,

Problem with me is my label is having oneline text without space or anything.

I am getting email id from a stored procedure and then i need to display the email id in the panel which i have added.

lblEmail.text = "jane.smith@bankofamerica.com" is the string and my label width and panel width i am setting to 140px.....
now the problem is suppose the text length of email id is more than that....something like "srinivasan.ramakrishnan@bankofamerica.com" then i want to display it as

srinivasan.ramakrishn
an@bankofamerica.com

in the label and panel.....
but it is not warpping up instead that panel width is getting increased.

how to wrap up in this case...please help me....
Left by sid on Nov 21, 2007 4:32 AM

# re: How to make your ASP.Net label multiline (how to wrap text in your label)
Requesting Gravatar...
excellent code, Thanks a lot
Left by ali raza on Nov 29, 2007 5:12 AM

# re: How to make your ASP.Net label multiline (how to wrap text in your label)
Requesting Gravatar...
Cool man!! it works..Thanks a lot.
Left by Anand on Jan 09, 2008 1:18 AM

# How I hide the scroll bar from the text box ??
Requesting Gravatar...
I need the text box as a ,label...And so i need to hide the scroll bars from the control. SomeOne please help me ....
Left by Tapas Mahata on Jan 11, 2008 12:49 AM

# re: How to make your ASP.Net label multiline (how to wrap text in your label)
Requesting Gravatar...
>>I need the text box as a ,label...And so i need to hide the scroll bars from the control. SomeOne please help me ....

I was already answered:
Style="overflow:hidden"
Left by Vlad on Jan 17, 2008 2:07 PM

# re: How to make your ASP.Net label multiline (how to wrap text in your label)
Requesting Gravatar...
an anybody help me out ....how to make wrapping of text of a level(asp controls) in mozilla.......

i tried this"WrappableText" function...but its not workin....
Left by Arindrajit on Jan 31, 2008 3:29 AM

# re: How to make your ASP.Net label multiline (how to wrap text in your label)
Requesting Gravatar...
you can also set the textbox backcolor to be transparent
by setting the backcolor property to web/transparent
Left by steve on Jan 31, 2008 10:38 AM

# re: How to make your ASP.Net label multiline (how to wrap text in your label)
Requesting Gravatar...
great stuff
Left by robin on Jan 31, 2008 1:21 PM

# re: How to make your ASP.Net label multiline (how to wrap text in your label)
Requesting Gravatar...
Long, your solution is great! Thank You guys
Left by Ben on Feb 02, 2008 9:20 AM

# re: How to make your ASP.Net label multiline (how to wrap text in your label)
Requesting Gravatar...
Noran - Thanks for the hide the scrollbar tip. Have been looking for that for a month.
Left by Randy on Feb 08, 2008 7:41 AM

# re: How to make your ASP.Net label multiline (how to wrap text in your label)
Requesting Gravatar...
Hey guys - Great Stuff Here on the label display / text box issues.

A couple of comments

1) Thanks for posting this to web - you really help the next guy down the line.

2) I've been messing around with labels for several years and I'm tired of babying them with a lot of text processing to offset the fact that they expand all over the place. I've been using text boxes instead of labels for a while now but only in areas where the scroll bars were OK. Thanks to this post I will be using them everywhere. In fact it will take a good reason for me to use a label in the futrue to display text.

3) Here are some additional tweaks you can make on managing text boxes that will save even more time and trouble. Some of them may have been mentioned above but I don't have the time to read through the many posts above....

1) Make several types of all purpose text box "labels" and save them on a page in your project you can copy them from. All of the tedious style setting will come along with them.

2)Take a few minutes to go through all of the style options by right clicking on the text box, if you do you will notice

TEXTBOXES - use the style options by right clicking a control and chosing "style" from the menu...

*can be centered left to righ and up and down -This is a great feature for asthetics. again - why use a label control for anything?

*will auto wrap on long unbroken string - which labels won't

*will auto truncate - in a really graceful way

*font sizes set in the in the style settings will not be overriden by a user changinng the font size in thier browser. - use "size - specific" 12pts etc.

*lots of other things - check them all out then make several workhorse labels of different sizes and styles on your stock page and paste it everywhere in your site, don't go back to the toolbox and start from scatch with an unknown - you will always know how it will react.

Finally I'll leave you with this cool little trick to make shaded fonts.

1) Create a panel control, turn the borders off, and set the backcolor to transparent - in other words make an invisible container.

2) Create a textbox with no border, transparent backcolor and whatever else it takes to hide it. Turn the scrolls off, auto center if you want (both done using the style menu). Past the text box IN the panel so that when you move the panel the text box moves with it.

3) Register the borders of the label to fill the interior of the panel - set the fore color property to WHITE.

4) Right click and copy the label - highlight the panel containing the first label and paste the clone into it.

5) Set the ForeColor property of the second label to the color of your choice - say black

6) Bump the Second label up and over so that it is registered directly on top of the first label - in other words the first label will disapear behind the one on top.

7) Use the arrow keys to bump the the top label left 1 and up one OR down one and right one.

8) In your code behind populate the labels as a pair like this

Dim OutputText as String
OutputText = "Hello"
OutputOneLbl.text = OutputText
OutputTwoLbl.text = OutputText

- to keep em in seek


9) When you're ready to use your 3-D label "object" be sure and copy the entire panel - this will keep the registration intact between the labels.


10) I've been putting all my controls in panel groups because when the panel is copied and pasted VisualStudio keeps them registered as oposed to Copy and pasting the controls themselves - wich VS mangles on the paste.


Live long and prosper...

ED








Left by Ed on Feb 13, 2008 2:39 PM

# re: How to make your ASP.Net label multiline (how to wrap text in your label)
Requesting Gravatar...
By the way - whay cant Microsoft put all of the properties in one place. As an interface designer I don't care that some are achieved by settting style properties and others are done some other way by the compiler - someone should make a third party add on that shows a property box with "auto center" etc along with the other properties.

Have you ever noticeds that if you set the height and width of a control you can do it in the property window - but if you want to set the top and left properties of the same control you have to go to the "style menu". I didn't even know that thing existed for a few monthsn - what's up with that.

Hey Microsoft - FIX THIS!!!
Left by Ed on Feb 13, 2008 2:45 PM

# re: How to make your ASP.Net label multiline (how to wrap text in your label)
Requesting Gravatar...
You can also use a label and in css use overflow:auto; This will add a scroll bar to the label if needed. You could also use the same css with the textbox to hide the scroll bar when not needed.

here is my css for that I can apply to either a label or textbox and get the same effects:

.notes
{
margin-bottom:5px;
color: #000000;
width: 365px;
height: 60px;
padding: 5px;
background-color: #fff;
background-repeat: repeat;
display: block;
overflow:auto;
}

<asp:label id="LabelNotes"
cssclass="notes"
runat="server" />

<asp:textbox id="TextBoxNotes"
cssclass="notes"
runat="server"
readonly="true"
textmode="MultiLine"
borderstyle="None"
borderwidth="0" />
Left by Deke on Feb 15, 2008 2:41 PM

# re: How to make your ASP.Net label multiline (how to wrap text in your label)
Requesting Gravatar...
I really like Bob's comment on 10/23/2007. I was wondering about something like that after seeing similar ideas in other languages (ruby on rails has a great helper for that). There should really be another conversion like ToString(), but ToHtml() instead. Ah well, there are always more things to wish for.
Left by Bri on Feb 20, 2008 7:26 PM

# re: How to make your ASP.Net label multiline (how to wrap text in your label)
Requesting Gravatar...
Thank'S For Solutions lbl.Text = Replace(lbl.Text, Chr(13) + Chr(10), "<br/>")
Left by Galih on Mar 04, 2008 10:34 PM

# re: How to make your ASP.Net label multiline (how to wrap text in your label)
Requesting Gravatar...
Thank you so much for this answer.

# re: How to make your ASP.Net label multiline (how to wrap text in your label)
<asp:Label Text='<%# Eval("Comment")==null ? Eval("Comment") : Eval("Comment").ToString().Replace("\n", "<br />") %>' runat="server" /> 10/23/2007 1:06 PM | Bob
Left by 007 From Thailand on Mar 18, 2008 7:03 AM

# re: How to make your ASP.Net label multiline (how to wrap text in your label)
Requesting Gravatar...
If you have Rich Text or text containing HTML tags, the multiline textbox will not diplat the formatted HTML text but will display the text alongwith the corresponding tags.
Left by Anonymous on Mar 19, 2008 5:16 AM

# re: How to make your ASP.Net label multiline (how to wrap text in your label)
Requesting Gravatar...
Thanks you sooooooo mush all people help us, i am search 3 days about this problem , its working

<asp:Label Text='<%# DataBinder.Eval(Container.DataItem,"Subject")==null ? DataBinder.Eval(Container.DataItem,"Subject") : DataBinder.Eval(Container.DataItem,"Subject").ToString().Replace("\n", "<br />") %>' runat="server" ID="Label1" NAME="Label2"/>
Left by Ahmad Ali on Apr 28, 2008 3:14 PM

# re: How to make your ASP.Net label multiline (how to wrap text in your label)
Requesting Gravatar...
Wrap in div or asp:Panel with width for basic wrapping... Any other solution seems quite silly...

Use Replace("\n","<br/>") for when you need to display the breaks that are included in the text to be displayed...
Left by Anonymous on May 02, 2008 3:51 PM

# re: How to make your ASP.Net label multiline (how to wrap text in your label)
Requesting Gravatar...
How to wrap the long sentance in label while i am type without space
Left by Valarmathi on May 14, 2008 2:18 AM

# re: How to make your ASP.Net label multiline (how to wrap text in your label)
Requesting Gravatar...
Hey guys,

the most usability solution is that of "JP" (6/25/2007 7:18 PM):

always text in always form you use ......Replace(Chr(13), "<br />").

This is OK!!!

Bye
Left by Luigi on Jun 14, 2008 7:23 PM

# re: How to make your ASP.Net label multiline (how to wrap text in your label)
Requesting Gravatar...
EXCELLENT...thank u very much
Left by anrao on Sep 06, 2008 4:14 AM

# re: How to make your ASP.Net label multiline (how to wrap text in your label)
Requesting Gravatar...
WrappableText worked great!!
thanks for sharing it..
If only i knew all about html formatting my life as a programmer would be lot easier!
Left by J on Dec 11, 2008 1:05 AM

# re: How to make your ASP.Net label multiline (how to wrap text in your label)
Requesting Gravatar...
Thanks Mihailo, the Panel with lable worked great! Plus I can do data binding, etc
Left by James on Jan 29, 2009 3:24 PM

# re: How to make your ASP.Net label multiline (how to wrap text in your label)
Requesting Gravatar...
Don't forget to change your Cursor from "IBeam" to "Default" as well!

Thanks for the helpful post.
Left by Mike Murray on Aug 16, 2009 12:40 AM

# re: How to make your ASP.Net label multiline (how to wrap text in your label)
Requesting Gravatar...
thanks guys, straight forward answers to a question that I have been battling with for a week not, thanks again.
Left by Jerry on Oct 08, 2009 7:27 AM

# re: How to make your ASP.Net label multiline (how to wrap text in your label)
Requesting Gravatar...
Using this style inside my asp:label control worked for me:

style="white-space: normal;">
Left by Tim on Nov 16, 2009 8:33 AM

# re: How to make your ASP.Net label multiline (how to wrap text in your label)
Requesting Gravatar...
Dear All,
I have seen people have different solutions but none of them given reliable and perfect solution except the "WrappableText()" method.
I would use that, don't be confuse with other solutions.

This is what I am doing:

protected void Page_PreRender(object sender, EventArgs e)
{
if (gvObjectivePlanning.Rows.Count > 0)
{
foreach (GridViewRow row in gvObjectivePlanning.Rows)
{
Label lblObjectives = (Label)row.FindControl(LABEL_OBJECTIVES);
lblObjectives.Text = WrappableText(lblObjectives.Text);

Label lblKPI = (Label)row.FindControl(LABEL_KPI);
lblKPI.Text = WrappableText(lblKPI.Text);


}

}
}

Just find the control in the preRender event and call the "WrappableText" method. Your problem is solved.

Note: Forget about using Textbox instead of lebel, It becomes difficult for you to find exact number of rows to be set dynamically for the textbox. which might work for some sort of data and might not for other sort of data.

Bye




Left by Ashraf Ansari on Dec 16, 2009 6:29 PM

# re: How to make your ASP.Net label multiline (how to wrap text in your label)
Requesting Gravatar...
in aspx
<% if(lbl1.visivle==true) { %>
<BR/>
<% } %>
Left by Amol on Jan 29, 2010 9:08 PM

# re: How to make your ASP.Net label multiline (how to wrap text in your label)
Requesting Gravatar...
Labels are used to display strings no matter how short or long they are. If the width property is not set for a label it is going to expand based on the string that is assigned to it's text property. We dont have to set any thing at all as a matter of fact. Just say <asp:Label runat="server" ID="lblTest">. They value may be assigned in the codebehind or in the tag itself like "this is a long string this is a long string this is a long string this is a long string this is a long string this is a long string this is a long string this is a long string this is a long string this is a long string". This string will itself be displayed in multiple lines.
Left by Rhoads on Feb 10, 2010 7:01 PM

# replace works
Requesting Gravatar...
lbl.Text = Replace(lbl.Text, Chr(13) + Chr(10), "<br/>")

It works!
Left by Neil on Feb 24, 2010 6:57 AM

# re: How to make your ASP.Net label multiline (how to wrap text in your label)
Requesting Gravatar...
It may not be the best way but I have always done it like this.

Dim message As String = "Hello World" & Environment.NewLine & "How are you?"

Label1.Text = TextBox1.Text.Replace(Environment.NewLine, "<BR/>");
Left by Victor on May 27, 2010 1:09 PM

# re: How to make your ASP.Net label multiline (how to wrap text in your label)
Requesting Gravatar...
I'd say that a asp:Literal should be used instead of a label or a textbox. A Literal is intended to contain markup, whereas a label is not. Try this snippet:

// Replace windows newlines then unix newlines.
string line = txtTest.Text.Replace("\r\n", "<br />");
line = line.Replace("\n", "<br />");
// litTest is an asp:Literal.
litTest.Text = line;
Left by avejidah on Jun 02, 2010 5:57 AM

# re: How to make your ASP.Net label multiline (how to wrap text in your label)
Requesting Gravatar...
WHY DON'T YOU USE Wrap="true" for label ???????????????????????????????

=)
Left by asmcad on Jun 29, 2010 1:48 PM

# re: How to make your ASP.Net label multiline (how to wrap text in your label)
Requesting Gravatar...
lblCadenaOriginal.Text = dr["CadenaOriginal"].ToString();
private string WrappableText(string source)
{
string _Sourse = "";
int _Contador = 0;

foreach(char chr in source)
{
_Sourse = _Sourse + chr;

_Contador++;

if (_Contador == 90) // letras por fila
{
_Sourse = _Sourse + "<br />";
_Contador = 0;
}
}

return _Sourse;
}
Left by GABRIEL JUAREZ SANDOVAL on Dec 02, 2010 7:43 AM

# re: How to make your ASP.Net label multiline (how to wrap text in your label)
Requesting Gravatar...
Thnks so much! That's what I looking for :X
Left by TrungNS on Mar 29, 2011 8:43 PM

# re: How to make your ASP.Net label multiline (how to wrap text in your label)
Requesting Gravatar...
how to get ride of the scrollbar?
Left by duez on May 05, 2011 1:48 AM

# re: How to make your ASP.Net label multiline (how to wrap text in your label)
Requesting Gravatar...
It helped me. Thanks !
Left by Ainee on Jun 15, 2011 11:21 PM

Your comment:
 (will show your gravatar)


Copyright © Jeff Tolle | Powered by: GeeksWithBlogs.net | Join free