7

We tried several ways to make a textbox to accept the "enter", newline, etc.. But we are still facing the same problems. Most of the "Third party" controls allow the user to format the text as he wants. Eg, add color, font, table, etc.. However, for most stylish websites, we do not want to allow the user to format the text that way.

But we still want them to make "enter", so we disable most functions (Colors, bold, table, insert image, etc.). But we still have another problem, copy and paste. It is not uncommon to see people that copy from MS Word in the textbox and wham, all the style of the site is awful!

That is why I turn on the possibility of making my own textbox, multiline (the ASP. Net) and just let the right to make press "Enter" (< br / >).

What is the best way to proceed?

Is there any tips that I have to watch out?

Thank you!

Simon Dugré
  • 17,980
  • 11
  • 57
  • 73
  • Possible duplicate of [ASP.Net Text with LineBreak from Multi-Line-TextBox to save in a database](http://stackoverflow.com/questions/4883613/asp-net-text-with-linebreak-from-multi-line-textbox-to-save-in-a-database) – Tot Zam Dec 31 '15 at 15:55

2 Answers2

14

Set the mode to TextBoxMode.MultiLine

Either in the code-behind,

myTextBox.TextMode = TextBoxMode.MultiLine

or in the markup

<asp:TextBox TextMode="MultiLine"

When the user enters text in the TextBox, it will come back to you with new lines as \r\n. If you'd like to display it properly to the user, you could use

myTextBox.Text.Replace(Environment.NewLine, "<br />")
Yuriy Faktorovich
  • 67,283
  • 14
  • 105
  • 142
  • I know Multiline!! But it do not accept enter! I mean, when a user edit his description and save it, back to the admin place, when he just want to see this description, if user press enter to make a line break, he will not see it! – Simon Dugré Oct 15 '10 at 17:05
  • 2
    @Simon Do you mean replace the enter keys he entered with
    for display?
    – Yuriy Faktorovich Oct 15 '10 at 17:07
  • Yea sorry, my english not realy great! I usualy talk french and explain what I want in english probably sometime sound weird !! – Simon Dugré Oct 15 '10 at 17:09
  • 1
    OK, juste saw your latest update. And same for when he come back to edit it again, I should replace the "
    " by "Environment.NewLine" se in the textbox he will see it normaly! Great ! Thank u!
    – Simon Dugré Oct 15 '10 at 17:14
  • replacing the new line with
    . This is what I was looking for. Thanks Yuriy
    – Prabo Oct 30 '17 at 14:39
1

To avoid this problem and allow HTML tags in TextBox control you need to change ValidateRequest of Page directive to false. You can do it like in code bellow:

use ValidateRequest="false"

Jitu
  • 11
  • 1
  • Yup good point. That was something I know before though. But thanks for the advice and for any other person who could see this post. – Simon Dugré May 02 '12 at 17:51