Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Textbox with "new line"

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!

like image 413
Simon Dugré Avatar asked Oct 15 '10 16:10

Simon Dugré


People also ask

How do you make a new line in a text box?

To start a new line of text or add spacing between lines or paragraphs of text in a worksheet cell, press Alt+Enter to insert a line break.

How do I insert a line break in a text box?

Press ALT+ENTER to insert the line break.

How do I add a new line in C#?

By using: \n – It prints new line. By using: \x0A or \xA (ASCII literal of \n) – It prints new line. By using: Console.


2 Answers

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 />")
like image 138
Yuriy Faktorovich Avatar answered Oct 20 '22 07:10

Yuriy Faktorovich


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"

like image 30
Jitu Avatar answered Oct 20 '22 07:10

Jitu