I’ve got a standard details view (ASP.NET 2.0 – VB). When I try retrieving text data from the database (SQL 2005), the carriage returns that were added are lost when I try to recall like so:
CRLF's do not render in a browser. You'd need to replace them with "<br />" during presentation.
I usually use a function to perform this kind of data "fixup".
Protected Function FixCrLf(value As String) As String
If String.IsNullOrEmpty(value) Then Return String.Empty
Return value.Replace(Environment.Newline, "<br />")
End Function
Protected Function FixCrLf(value As String) As String
If value Is Nothing OrElse value.Length=0 Then Return String.Empty
Return value.Replace(Environment.Newline, "<br />")
End Function
I've just had this problem until I found this post. It was fine having the html encoded into the data when I was just displaying the data. When it came round to maintaining the data in a gridview I ran into the problems of not being able to update html
encoded data. I tried a few options of trying to replace the html code on text editing and replacing on update. That didn't seem to work. I didn't want to go and set validaterequest to false or do any messy coding. I then noticed that multiline textboxes
could render carriage returns but labels were not. Surely there was a way at least to read the carriage returns. Then I find this post - a simple solution to the problem and without encoding the data with HTML (except for rendering).
RichardLaw
Member
464 Points
626 Posts
Carriage returns not displaying in a details view label
Jan 02, 2007 02:23 PM|LINK
Hi
I’ve got a standard details view (ASP.NET 2.0 – VB). When I try retrieving text data from the database (SQL 2005), the carriage returns that were added are lost when I try to recall like so:
<asp:Label ID="diaryEventBody" runat="server" Text='<%# Bind("postEventFeedback") %>' Width="605px"></asp:Label>
I know they’re still there because if I copy and paste from the database into notepad, they aprear fine.
So what do I need to do to show them in the code above?
Many thanks
Richard
mbanavige
All-Star
134961 Points
15421 Posts
ASPInsiders
Moderator
MVP
Re: Carriage returns not displaying in a details view label
Jan 02, 2007 03:05 PM|LINK
CRLF's do not render in a browser. You'd need to replace them with "<br />" during presentation.
I usually use a function to perform this kind of data "fixup".
Now your databind would get changed to:
Note: post edited to correct missing keyword "String" as mentioned in later post. (Thanks Richard)
Darmark
Contributor
3843 Points
709 Posts
Re: Carriage returns not displaying in a details view label
Jan 02, 2007 03:07 PM|LINK
I may be wrong but i don't think Label controls reads carriage returns.
Mark as Answer, if this reply answers your post.
RichardLaw
Member
464 Points
626 Posts
Re: Carriage returns not displaying in a details view label
Jan 02, 2007 09:25 PM|LINK
Hi mbanavige,
Thanks for getting back, only when I paste the VB code into my code behind for the page, it has a problem with 'IsNullOrEmpty' - Name not declared???
Any thoughts?
Thanks
mbanavige
All-Star
134961 Points
15421 Posts
ASPInsiders
Moderator
MVP
Re: Carriage returns not displaying in a details view label
Jan 02, 2007 09:50 PM|LINK
IsNullOrEmpty is new to the 2.0 framework
For 1.1 you could do it like this:
RichardLaw
Member
464 Points
626 Posts
Re: Carriage returns not displaying in a details view label
Jan 02, 2007 09:55 PM|LINK
Thank so much.
I found that for 2.0, all I needed to do is add 'string.' in front:
If
String.IsNullOrEmpty(value) Then Return String.EmptyThis works fine in 2.0, and to come back to Darmark's comment earlier, it also works with lables too!
Thanks all
Calmack
Member
2 Points
2 Posts
Re: Carriage returns not displaying in a details view label
Apr 29, 2009 10:59 AM|LINK
I've just had this problem until I found this post. It was fine having the html encoded into the data when I was just displaying the data. When it came round to maintaining the data in a gridview I ran into the problems of not being able to update html encoded data. I tried a few options of trying to replace the html code on text editing and replacing on update. That didn't seem to work. I didn't want to go and set validaterequest to false or do any messy coding. I then noticed that multiline textboxes could render carriage returns but labels were not. Surely there was a way at least to read the carriage returns. Then I find this post - a simple solution to the problem and without encoding the data with HTML (except for rendering).
Many Thanks