Carriage return problem in ASP.NET

andycharger

Centurion
Joined
Apr 2, 2003
Messages
152
Hi.

Im storing the results of my textarea on my webform to a SQL server database. I have written the following bit of code to remove the chr(10) and chr(13) and replace them with vbcr.

strBody = request.form("txtNews")
strBody = Replace(strBody, chr(10), "")
strBody = Replace(strBody, chr(13), "$$")
strBody = Replace(strBody, "$$", vbcr)

I hoped this would then allow my item to be viewed with the line breaks in the right place.

However, when I view the results as the news item on the webpage, it is on one continuous line. If I go to the edit page and view it in the Textarea editor, it is correct on separate lines.

I want to show it in a <td> cell with the line breaks beween the sentences. How can I do this?

Im using a <asp:datalist> to show the results and my <td> in this looks like this:

<td class="normaltext">
<% # DataBinder.Eval(Container.DataItem, "News_text")%>
</td>

Can anyone help?
 
To display a line break in HTML you need add the HTML tag <BR> for a line break and a <P> for a line break with a space. Browsers do not recognize CR/LF. That makes it easier for you to view source. You can also replace the cr/lf together:

strBody = Replace(strBody, chr(13) + chr(10), "<BR>" + vbCr)
 
Thanks but....

It does not work!

The value of strBody comes back as nothing when doing the insert into the databse.

However, I dont think you need to put the <br> into the database, just when writing it to the screen in the <td> element. However, Im using a datalist and need to know how to do the formatting of the cell before it gets to the datalist. I dont think you format it in the datagrid, I think you do it before. Perhaps in the recordset?

I create my recordset like this:
'Put user code to initialize the page here
Dim strDept As String
Dim oConn As SqlConnection
Dim oComm As SqlCommand
Dim oReader As SqlDataReader
Dim sSQL As String
Dim sConn As String
Dim strID As String

strID = Request.QueryString("news_id")

sSQL = "SELECT * FROM news where news_id=" & strID
sConn = ConfigurationSettings.AppSettings("ConnectionString")

oConn = New SqlConnection(sConn)
oConn.Open()

oComm = New SqlCommand(sSQL, oConn)
oReader = oComm.ExecuteReader()

dgnews.DataSource = oReader
dgnews.DataBind()

oConn.Close()


So how would I format the news_text item in my dgnews recordset for displaying as HTML?


Sorry for all the questions!

Andy
 
Its check each character’s ASCII value entered into the textbox, so when the enter key is pressed its also check the ASCII value and then it avoid to insert the carriage return value to the textbox. So we can avoid to insert Carriage Return into the textbox having “ Multiline” TextMode property or in TextArea control.

<asp:TextBox ID="txtComments" Height="150" Columns="200" TextMode="multiline" MaxLength="4000" runat="server" onkeydown = "return (event.keyCode!=13);" />

Note: ASCII value for Enter Key is 13.

Restrict to insert carriage return into Textbox control
 
Back
Top