33

I set a value for a Multiline Textbox like this.

textBox1.Text = "Line1\r\n\r\nLine2";

But, only one line space in output.

When I read the value of textbox, I read "Line1\r\nLine2";

Why does ASP.NET not support more then one lineline character?

ag93
  • 343
  • 4
  • 17
selami
  • 2,478
  • 1
  • 21
  • 25

7 Answers7

51

You need to set the textbox to be multiline, this can be done two ways:

In the control:

<asp:TextBox runat="server" ID="MyBox" TextMode="MultiLine" Rows="10" />

Code Behind:

MyBox.TextMode = TextBoxMode.MultiLine;
MyBox.Rows = 10;

This will render as a <textarea>

Tom Gullen
  • 61,249
  • 84
  • 283
  • 456
  • 1
    I set it to multiline. But, I cannot set programatically, multiple line space to the textbox. – selami May 06 '11 at 10:13
17
textBox1.Text = "Line1" + Environment.NewLine + "Line2";

Also the markup needs to include TextMode="MultiLine" (otherwise it shows text as one line)

<asp:TextBox ID="multitxt" runat="server" TextMode="MultiLine" ></asp:TextBox>
mas_oz2k1
  • 2,851
  • 3
  • 34
  • 41
V4Vendetta
  • 37,194
  • 9
  • 78
  • 82
  • which broswer are you using, i checked this in IE7 and FF works perfect. I hope you have typed the above thing properly ! – V4Vendetta May 06 '11 at 11:17
  • I check this with IE 8 and Google Chrome. Same result. – selami May 06 '11 at 11:27
  • The is no update panel. But, when I put the textbox in updatePanel and set the value of textbox when a submit button click, TextBox set correctly. You can try this on your development environment. Strange situation, maybe a bug. – selami May 06 '11 at 11:40
  • Actually, when page is postback, there is no problem. – selami May 06 '11 at 12:36
4

Try this one

textBox1.Text = "Line1" + Environment.NewLine + "Line2";

Working fine for me...

Harpal
  • 1,729
  • 17
  • 18
  • Yo can use multiple **Environment.NewLine** – Harpal Nov 03 '11 at 07:15
  • IE renders just one new line. try it. – selami Nov 03 '11 at 21:17
  • I'm using `txtMultiline.Text = "Line1" + Environment.NewLine + "Line2" + Environment.NewLine + "Line3";` and its working fine in all browsers – Harpal Oct 05 '12 at 06:43
  • Only one Environment.NewLine is OK, but multiple Environment.NewLine not working. For example; txtMultiline.Text = "Line1" + Environment.NewLine + Environment.NewLine + Environment.NewLine + "Line2"; – selami Oct 05 '12 at 08:13
  • I'm using `txtMultiline.Text = "Line1" + Environment.NewLine + Environment.NewLine + Environment.NewLine + Environment.NewLine + "Line2" + Environment.NewLine + "Line3";` and its working fine – Harpal Oct 09 '12 at 06:16
3

I had the same problem. If I add one Environment.Newline I get one new line in the textbox. But if I add two Environment.Newline I get one new line. In my web app I use a whitespace modul that removes all unnecessary white spaces. If i disable this module I get two new lines in my textbox. Hope that helps.

TimW
  • 78
  • 5
2

When page IsPostback, the following code work correctly. But when page first loading, there is not multiple newline in the textarea. Bug

textBox1.Text = "Line1\r\n\r\n\r\nLine2";
Jesuraja
  • 3,774
  • 4
  • 24
  • 48
aliye
  • 21
  • 1
0

textBox1.Text = "Line1\r\r\Line2";
Solved the problem.

senthilkumar2185
  • 2,536
  • 3
  • 22
  • 36
selami
  • 2,478
  • 1
  • 21
  • 25
0

While dragging the TextBox it self Press F4 for Properties and under the Textmode set to Multiline, The representation of multiline to a text box is it can be sizable at 6 sides. And no need to include any newline characters for getting multiline. May be you set it multiline but you dint increased the size of the Textbox at design time.