Purplegoldfish
Purplegoldfish

Reputation: 5294

How can I change the linebreaks from a multiline textbox to HTML <br /> tags?

Im working on a bulk email sending app and have a page with a multiline textbox which I am wanting to use as a way of allowing a user to enter the email body text.

I need to format the text within my textbox to HTML, most importantly I need to format linebreaks to HTML, however I cant seem to do this.

The method everyone seems to say to use is:

textOut.Replace("\r\n", "<br />")

But this just does nothing. My Textbox looks like this:

   <asp:TextBox runat="server" 
                            ID="txtMailBody" 
                            TextMode="MultiLine" 
                            Width="650" 
                            Height="150"/>

When I enter text in the textbox such as:

Line 1

Line 2
Line 3

It always outputs like

Line 1 Line 2 Line 3

Am I doing something wrong here?

Upvotes: 1

Views: 9901

Answers (5)

cavalcanteg
cavalcanteg

Reputation: 800

I had the same issue. It worked when I used the following code:

textOut.Replace(System.Environment.NewLine, "<br />")

Upvotes: 1

Sunil Raj
Sunil Raj

Reputation: 460

txtMailBody.Text.Replace("\n", "<br />")

This may help you fix.

Upvotes: 1

horstfh
horstfh

Reputation: 61

Alle three lines below do it:

TextBox2.Text = TextBox1.Text.Replace(vbLf, "<br>" + vbCrLf)

TextBox2.Text = TextBox1.Text.Replace(vbCrLf, "<br>" + vbCrLf)

TextBox2.Text = TextBox1.Text.Replace(vbNewLine, "<br>" + vbCrLf)

Upvotes: 6

Ninad Ajnikar
Ninad Ajnikar

Reputation: 173

There is a function in php named as nl2br you can search for an asp alternative to it.

Upvotes: -1

Sebastian Siek
Sebastian Siek

Reputation: 2075

Try this

txtMailBody.Text.Replace(Environment.NewLine, "<br />")

Hope it helps

Upvotes: 3

Related Questions