Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Rhinorhino on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Text field won't display correctly 2

Status
Not open for further replies.

Andel

Programmer
Joined
Feb 15, 2001
Messages
366
Location
US
Hello, I have a text field which hold a whole body of an Article. When I display this field using DataList, it becomes 1 paragraph. Seems like the 'enter' key disappear.

In classic ASP, I have this function to resolve that.

<%
Function PrepForHtml (strDirty)
Dim strClean
If IsNull(strDirty) OR Trim(strDirty) = "" Then
PrepForHTML = ""
Exit Function
End If
strClean = Trim(strDirty)
strClean = Replace(strClean, "<", "<", 1, -1 , 1)
strClean = Replace(strClean, ">", ">", 1, -1 , 1)
strClean = Replace(strClean, Chr(10), "<br>", 1, -1, 1)
PrepForHtml = strClean
End Function
%>

How would I do the same thing in ASP.NET?

Thanks,
Andel


 
If you wish to replicate the example function you posted, you can pretty much use it "as is" but I'd use the Replace method of the actual string e.g.
Code:
myString = myString.Replace("a","b")
However, if you are just looking to write the contents out to the page, you could consider putting it inside a textarea (as this should preserve the line breaks) or as I think you may be attempting to do this in the function you posted, use the Server.HtmlEncode method.


____________________________________________________________

Need help finding an answer?

Try the Search Facility or read FAQ222-2244 on how to get better results.

 
IMHO - It might not be the best idea to pump an entire article into a data entry control. If you are already planning on creating a function to format your article into a simple html version (ie "<br>" etc) then think about using a literal control and set its Text property to your html article text.

Senior Software Developer
 
Ok, it's working now.
How would I make that function available to all the pages? I want to be able to call that function wherever i need it.


 
How would I make that function available to all the pages? I want to be able to call that function wherever i need it.
Create it as a public method of a class and pace the class in the App_Code folder.

IMHO - It might not be the best idea to pump an entire article into a data entry control.
For what reasons?


____________________________________________________________

Need help finding an answer?

Try the Search Facility or read FAQ222-2244 on how to get better results.

 
For what reasons?"

Mostly elegance and ease of read for a user, but it will also post all of that data back to the server on a submit.

Senior Software Developer
 
I tried it but i have an error message that says: "FormatHTML is a type and cannot be used as an expression"

Here's the code i placed in app_code folder:

Public Class FormatHTML
Private Function FormatHTML(ByVal strDirty)
Dim strClean
If Trim(strDirty) = "" Then
FormatHTML = ""
Exit Function
End If
strClean = Trim(strDirty)
strClean = Replace(strClean, "<", "<", 1, -1, 1)
strClean = Replace(strClean, ">", ">", 1, -1, 1)
strClean = Replace(strClean, Chr(10), "<br>", 1, -1, 1)
FormatHTML = strClean
End Function
End Class

Here's the code in the vb page of the page i'm working on:

Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load

Dim dv As New System.Data.DataView
Dim dt As New System.Data.DataTable
dv = SqlDataSource_AmadoYoroOnline.Select(DataSourceSelectArguments.Empty)
dt = dv.ToTable()
Label1.Text = dt.Rows(0).Item("ArticleTitle")
Label2.Text = FormatHTML(dt.Rows(0).Item("ArticleBody"))

End Sub



 
Mostly elegance and ease of read for a user, but it will also post all of that data back to the server on a submit.
Only if viewstate is enabled on that control.
 
Mostly elegance and ease of read for a user
The textarea can be styled however you want so it can easily be made to look exactly the same as if you used a "literal control and set its Text property to your html article text".



____________________________________________________________

Need help finding an answer?

Try the Search Facility or read FAQ222-2244 on how to get better results.

 
Only if viewstate is enabled on that control."

Not entirly true. You are right that turning off the viewstate will help in reducing the page size, but if the page is being submitted then you are returning all of the article text as part of the request because it is inside of an input control.


"The textarea can be styled however you want so it can easily be made to look exactly the same as if you used a "literal control and set its Text property to your html article text"."

When I say elegance, I mean that it is limited to one type of font format. It's limiting. Why would you not use a Literal control for this? Then you can use html formatiing now and in the future (add formatted sub headings, links, bold underlined or italic font, etc). It's just a helpful recomendation and my opinion, I'm not trying to be a jerk or anything. ok?

----------------------
Anyway, Andel... I think you are geting this error because the following line is referecing your class and not your function.
Label2.Text = FormatHTML(dt.Rows(0).Item("ArticleBody"))


change to somthing like:

Public Class clsFormatHTML
Public Function FormatHTML(ByVal strDirty As String) As String
Dim strClean
If Trim(strDirty) = "" Then
Return ""
Exit Function
End If
strClean = Trim(strDirty)
strClean = Replace(strClean, "<", "<", 1, -1, 1)
strClean = Replace(strClean, ">", ">", 1, -1, 1)
strClean = Replace(strClean, Chr(10), "<br>", 1, -1, 1)
Return strClean
End Function
End Class


Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load

Dim dv As New System.Data.DataView
Dim dt As New System.Data.DataTable
dv = SqlDataSource_AmadoYoroOnline.Select(DataSourceSelectArguments.Empty)
dt = dv.ToTable()
Label1.Text = dt.Rows(0).Item("ArticleTitle")
Dim oFormater as new clsFormatHTML()
Label2.Text = oFormater.FormatHTML(dt.Rows(0).Item("ArticleBody"))

End Sub


Senior Software Developer
 
When I say elegance, I mean that it is limited to one type of font format.
I'm not sure what you mean by this. The font inside a textarea can be changed with CSS.

Why would you not use a Literal control for this?
There are reasons not to use a Literal control such as you have to convert line breaks and you might want to allow the user to edit the details.

Then you can use html formatiing now and in the future (add formatted sub headings, links, bold underlined or italic font, etc).
Oh, I agree that it is a good solution but bear in mind the OP has a function that they've written in ASP and are just wanting to convert it. I assumed (either wrongly or rightly as this information wasn't provided by the poster, so assumptions had to be made) that they didn't want to display any HTML formatting and that they just wanted to preserve line breaks (which is all their function seems to do).

It's just a helpful recommendation and my opinion, I'm not trying to be a jerk or anything. ok?
Don't worry, I didn't take it that way - I agreed with what you said but I was genuinely interested in what the reasons were for your statement.


____________________________________________________________

Need help finding an answer?

Try the Search Facility or read FAQ222-2244 on how to get better results.
 
All I meant is that a TextArea can only contain one type of font format, so making a later enhancment like adding bold to a single word would not be possible. But you are right, maybe they have no intention of ever needing anything like that.

anywho ... We cool!!! LOL! ;o)

Senior Software Developer
 
you guys are awesome!

 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top