Vinz' Blog

"Code, Beer and Music" ~ my way of being a programmer!
posts - 124, comments - 366, trackbacks - 0

My Links

News

Archives

Image Galleries

Display Contents of MultiLine TextBox to Label Control and Retain its Line Breaks

This example demonstrates on how to print or display the contents of the MultiLine TextBox in a Label Control and retain its line breaks.

Basically MultiLine Mode of TextBox uses Environment.NewLine to create a new line. So if you try to display the values of TextBox in  a Label Control or print it on the page and retain its position then you can replace the  Environment.NewLine with <BR> Tag since Label control doesn't recognized Environment.NewLine.

Here's a sample demo  below for your reference:

ASPX

<asp:TextBox ID="TextBox1" runat="server" Height="300px" Width="300px" TextMode="MultiLine"/>

<asp:Button ID="Button1" runat="server" OnClick="Button1_Click" Text="Print to Label Control"/>

<br />

<asp:Label ID="Label1" runat="server" Text="Label" Height="300px" Width="300px"/>


RELEVANT CODES 

protected void Button1_Click(object sender, EventArgs e)

{

        Label1.Text = TextBox1.Text.Replace(Environment.NewLine, "<BR/>");

}

and here's the page output below

Print | posted on Tuesday, February 17, 2009 5:04 PM |

Feedback

Gravatar

# re: Display Contents of MultiLine TextBox to Label Control and Retain its Line Breaks

This doesn't work for me.
I have a multiline textbox, and whenever i tried replacing Environment.NewLine with "<BR/>" but to add an agony it doesnt even replace the strings. Are you sure its Environment.NewLine only.
2/24/2009 11:17 PM | Nirman Doshi
Gravatar

# re: Display Contents of MultiLine TextBox to Label Control and Retain its Line Breaks

Hi Nirman,

What are you trying to achieve? Do you want to display the contents of the MultiLine TextBox in a Label? Could you please elaborate?
2/25/2009 1:57 AM | Vinz
Post A Comment
Title:
Name:
Email:
Website:
Comment:
Verification:
 
 

Powered by: