Monday, March 26, 2012

maintain line breaks in a textbox/database

hi,

i have a text box in a formview on a VB page. this text box is used to submit news and events for a company. when the user who is entering in an article finishes a paragraph and wants to move on to the next, they will enter a line space (<br>) for the next paragraph...

now, i dont want them to have to enter in <br> everytime, so how is it possible to maintain the line spaces when a user presses enter? im not sure if its something on the control or databases's end?

all help is greatly appreciated!

One simple way is...

Replace the <enter> with a br, then read it back into a label or differently into a textbox...

Into the db: FixNewLine

Reading db into textbox: GetNewLine

Reading db into label: no function needed.

PublicSharedFunction FixNewLine(ByVal strValueAsString)AsString

FixNewLine = Replace(strValue, vbCr,"<br>")Return FixNewLine

EndFunction

PublicSharedFunction GetNewLine(ByVal strValueAsString)AsString

GetNewLine = Replace(strValue,"<br>", vbCr)Return GetNewLine

EndFunction

One problem, <br> is now <br />

Zath


Hi,

I actually used:

<%# Eval("DataItem").Replace(Environment.NewLine,"<br />")

whichworked perfectly for me...but only on my data whose datatypes werevarchar. I have one data item that is ntext and I try the same thingfor that item, and it just doesn't work. I don't get an error, oranything like that...it just doesn't show the carriage spaces.

I'm trying to do this in a label control, with this exact code:

<asp:Labelid="lblFavoriteQuotesText" runat="server" CssClass="TabsRightSide"Text='<%# Eval("FavoriteQuotes").Replace(Environment.NewLine,"<br/>")%>'></asp:Label>

Does anyone have any idea why this doesn't work using ntext but dos for varchar?


Try like this,

Server.HtmlEncode(lblFavoriteQuotesText.Text).Replace("\n","<br/>");

Dont forget to Mark as 'Answered', if you got the solution.

0 comments:

Post a Comment