I already tried using 'newtext = text.Replace("\n", "<br />")', but unfortunately the line breaks are still ignored. I have been searching for "\n" in the text paragraphs using 'indexOf', but it still could not find any line breaks (result: -1).
Does somebody have a hint? Thanks!Can you post the method in which the 'newtext = text.Replace("\n", "<br />")' line of code is?
If a user types text into a textbox and the text "wraps", those arenot line breaks.
Could that be the problem?
Yes, I use 'wraps'... should I use the HTML control 'Textarea' instead?
I have two aspx files: 'first.aspx' and 'output.aspx', which code can be seen here under. The purpose is to create a HTML file. The first file put the text into the database, and the second file creates a HTML file of the regarding text.
(in first.aspx)
Dim text2 As TextBox = New TextBox
text2.ID = "text2"
text2.TextMode = TextBoxMode.MultiLine
text2.Columns = 40
text2.Rows = 3
text2.Wrap = True
Panel1.Controls.Add(text2)
(regarding text in MSSQL)
blah blah blah blah
blah blah blah blah
(in output.aspx)
newText = text.Replace("\n", "<br />")
FileWriter.WriteLine("<p><h5>" + newText + "</h5></p>")
(in the created file: 'newfile.html')
<P>
<H5>
blah blah blah blah
blah blah blah blah
</H5>
</P>
Well, text wraps will not be "saved" as newlines. So the text.Replace you've shown will only work if the user actually pressed Enter to create a newline.
If you want the output page's <h5> to wrap the text, then you'll need to give the <h5> a "width" ... so that it wraps in a similar place as the input page's textbox.
FileWriter.WriteLine("<p><h5 style=""width:200px"">" + newText + "</h5></p>")Does that help?
thanks for your reply... unfortunately it does not help. I pressed 'Enter' to create newlines, but it does not work.
About the second point: the width should not make difference since the user creates the newlines.
Maybe it is because of the 'Data Type' of the MSSQL, although I tried 'text' and 'varchar', but both of them does not work.
In that case, try usingHttpUtility.Encode before saving the text to your database.
Then, after retrieving this text from the database, apply theHttpUtility.Decode method to the text.
Then, finally, run your newline replace method, to insert the necessary <br /> tags.
tnx again for reply, and again, it does not help... maybe the cause can be found in the functions that puts the text into and out of the database, as seen below...
Again, the 'newlines' are displaying in the database itself (empty line). Besides, the empty lines also appear in the output file.
['put into database' function]
Private Sub newFunction2(ByVal sender As System.Object, ByVal e As System.EventArgs)
Dim MyConnection As SqlConnection, 'etc
Dim text2 As TextBox = CType(Page.FindControl("text2"), TextBox)Dim text2Text As String = HttpUtility.HtmlEncode(text2.Text)
MyConnection = New SqlConnection(connectionstring)
MyCommand = New SqlCommand("insert into buffer1 (type,text) VALUES ('text','" & text2Text & "') ", MyConnection)MyCommand.Connection.Open()
MyCommand.ExecuteNonQuery()
MyCommand.Connection.Close()End Sub
['get from database' function]
Private Sub Page_Load(ByVal sender ...
'etc etc
For Each DR In DS.Tables(0).Rows
Dim type As String = DS.Tables(0).Rows(i)("type").ToString()
Dim text As String = DS.Tables(0).Rows(i)("text").ToString()If type = "text" Then
Dim text2 As String = HttpUtility.HtmlDecode(text)
text2Text = text2.Replace("\n", "<br />")
FileWriter.WriteLine("<p><h5>" + text2Text + "</h5></p>")
'etc etc
Hang on. I think\n is a C# construct. I don't think it will work in VB.NET.
Try using the following line instead:
text2Text = text2.Replace(System.Environment.Newline, "<br />")
Yes. That was the missing link. Thanks!
0 comments:
Post a Comment