tags:

views:

238

answers:

4

Is it possible to break label to new lines if the contents exceeds label's width? (like it happens in multiline textbox) I know there is a word wrap css, but it's not cross-browser.

Or I need to break it manually, by inserting <br> tag every x chars like I do it now (maybe there is a better method):

 string  content = HttpUtility.HtmlEncode(content);
 int len = content.Length; 
 for (int i = 80; i < len; i = i + 80)
 {                       
     content = content.Insert(i, "<br>");                      
 }
 return content;
A: 

I don't think there is a property of a asp:label to do what you need. But you are doing generally what I do when I need something like this!

If you can stick the label inside of a HTML element that you can set the width of, then the css will take care of word wrapping for you, IF your text is made up of text with spaces:

    <div style="width:80px"><asp:label id="lbl" runat="server" 
text="abcdefghij klmnopqrstuvwzyz1234567890 
abcdefghijklmnopqrstuvwzyz12345678 
90abcdefghijklmnopqrstuvwzyz1234567890ab 
cdefghijklmnopqrstuvwzyz123 
4567890abcdefghijklmnopqrstu 
vwzyz1234567890abcdefghijkl 
mnopqrstuvwzyz1234567890abcdefgh
ijklmnopqrstuvwzyz1234567890" /></div>

But sometimes if the text doesn't have anything that the browser detects as a word-break, it'll just spit out the entire string in one huge line that breaks your design.

So I usually manually split large strings if I need to, else I'll rely on CSS to do it for me.

Lanceomagnifico
A: 

The thing that is cross browser compatible is changing your label to a block, then you don't have to do any server side processing. My guess is that what ever renders out, renders out in a <span /> so the width doesn't work.

try this

span.mylablel {
    display: block;
    width: 100px;
}

hope this helps

Nick Berardi
I've added this style="display: block; width: 100px;" to the label. didn't effect.
Can you post the generated HTML. It is hard to give a solution when you don't give the output that is causing your problems and the only code you give is a question of if you should do it another way.
Nick Berardi
A: 

There's no property that will do this for a label. Instead of using a label why not use a TextBox and make it look like a label? You would then gain the built-in multiline functionality. It would be a clean solution and not require code manipulation to perform the line breaks.

Try using a textbox control with the following properties set, perhaps it will fit your needs:

    <asp:TextBox ID="txtFakeMultilineLabel" runat="server" BorderStyle="None"
BorderWidth="0" Wrap="true" Rows="14" Width="300" ReadOnly="true"
TextMode="MultiLine">text here or set from code-behind</asp:TextBox>

You would set the rows and width to control the desired size. If the content spills past the rows then scrollbars will appear. This may or may not be desirable but it is a quick solution.

Ahmad Mageed
I can't use textbox becasue I don't want the cursor to appear inside textbox (ReadOnly="true" doesn't remove it), and I don't want the right-button context menu of textbox to appear.
You could disable the cursor and context menu by disabling the textbox. The downside of this is that the text can't be selected by the user (to copy/paste for example). And you would need to update colors to get around the disabled look. To try it out add these properties: Enabled="false" BackColor="White" ForeColor="Black"Like I said, this is a quick solution and it isn't very flexible if you need more out of it.
Ahmad Mageed
A: 
Sypress
If I just set "width : 100px" it doesn't effect if string is a long string without any spaces.
Look at the updated version
Sypress