views:

143

answers:

5

I get the data from the database and display that data on the label. I have to apply new line on the text of the label for every 35 charecters. Because after 35 charecters the text overflows.

Can you please let me know how to do this.

A: 
use Environment.NewLine
Asad Butt
browser will ignore it, it is html
Andrey
A: 

there is no way to do this, you would better create more labels. you could add <br/>, but asp.net will screen it. consider putting single label inside div with fixed width, the text should go to new line automatically.

Andrey
+3  A: 

Just change the style of the label so it will wrap the text:

style=" width:50px; overflow-y:auto;overflow-x:auto; word-break:break-all;"

Better yet, put this in your CSS file rather than directly on the control.

Another option, if you don't like this, is to use a textbox and style it as a label, as described in this post.

Oded
Using text box like a label works. Thanks.
xrx215
I've got validation errors in CSS 2.0 and 2.1. Is "word-break" CSS 3.0 ?
Slauma
@Slauma - Yes, it is CSS 3.0.
Oded
A: 
//assuming l is a label
for (int i = 0; i < l.Text.Length; i++)
{
  if (i % 35 == 0)
    l.Text.Insert(i, Environment.NewLine) // or use "<br />" for html break since browsers ignore whitespace (except IE 6 in some cases)
}
MasterMax1313
A: 

quick psuedo code

string s = 'value from db';
string s2 = "";
int len = s.Length;
int i = 0;
while ( i + 35 > len ) {
  s2 += s.Substring( i, 35 ) + "\r\n";
  i+=35;
}
s2 += s.Substring( i, len - i );
label.Text = s2;

if \r\n does not work, replace it with < br >

JDMX