Community discussion forum

How to add a newline with in a string being set to a label [C# 2005]

  • 1 year ago

    I am trying to find a way to introduce a NEW LINE into a string I am setting for the text of a label, specifically I want the label to span 3 lines... I've tried using "\n" but it doesn't seem to work at all, this is my current code...

    [Code]
    lblAddress.Text = province.ToString() + " - " + city.ToString() + " - " + address.ToString() + " - " + postal_code.ToString();
    [/Code]

    What I want to do is set the ADDRESS on another line and the POSTAL_CODE on another line so that the text of the label spans 3 lines, something like the following:
       Ontario Toronto
       123 Mission Street
       D4C 4D4

    [Code]
    lblAddress.Text = province.ToString() + " - " + city.ToString() + "\n" + address.ToString() + "\n" + postal_code.ToString();
    [/Code]

    But this doesn't work ... any clue why?
    This is to be used with an ASP:LABEL in ASP.NET 2.0 (not a form label) - incase this makes any difference...

    Any help would be greatly appreciated...
    Thanks,

     

  • 1 year ago

    Maybe you can just use <br/> tags to achieve the line break:


    [Code]
    lblAddress.Text = province.ToString() + " - " + city.ToString() + "<br/>" + address.ToString() + "<br/>" + postal_code.ToString();
    [/Code]

  • 1 year ago

    char : (char)13   // '13' is ASCII code of newline

    string : System.Environment.NewLine

  • 1 year ago

    Mohammad,

    Although (char)13 and Environment.NewLine will give you a new line, I believe that they will behave in the same way as "\n" which doesn't render in an asp:label for the OP. This is the reason why I suggest "<br/>.

    Tim

     

     

Post a reply

Enter your message below

Sign in or Join us (it's free).

Want to stay in touch with what's going on? Follow us on twitter!