Thursday, September 18, 2008

How can I maintain the format of text in a multiline textbox when saving to sql server ie new lines paragraphs etc in ASP.NET

Very common scenario - Here are my Thoughts

1. If you are using multiline text box (built in asp.net) - You can only save the New lines.

here is a quick c# code.

string FinalString = MultilineTextBox.Text.Replace("\r\n","");

and then save it in regular fashion in SQL server. Make sure to turn off the page validation request. i.e. ValidateRequest="false" at page level.

-------------------------------------------------------------------------------

2. Very Elgant, Less time consuming, and FREE way to do it.

a. Go to Free Text Box and download.
b. Put the DLL in Bin of your website.
c. Add a tag in your page.

<FTB:FreeTextBox id="txtDesc"
ToolbarLayout="ParagraphMenu, FontFacesMenu, FontSizesMenu, FontForeColorsMenu,
FontForeColorPicker, FontBackColorsMenu, FontBackColorPicker, Bold, Italic, runat="Server" Width="800px"/
>


d. This is server side control and directly give you HTML when you acesses the Text property in code behind. Save it in the SQL server as nvarchar and you are good to go.

thanks.

(Leave a comment if this helped you)

No comments: