Wrox Programmer Forums
Go Back   Wrox Programmer Forums > ASP.NET and ASP > ASP.NET 2.0 > ASP.NET 2.0 Basics
|
ASP.NET 2.0 Basics If you are new to ASP or ASP.NET programming with version 2.0, this is the forum to begin asking questions. Please also see the Visual Web Developer 2005 forum.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the ASP.NET 2.0 Basics section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old March 15th, 2006, 12:21 PM
Friend of Wrox
 
Join Date: Oct 2005
Posts: 173
Thanks: 0
Thanked 2 Times in 1 Post
Default Replacing Garriage Returns with <br />

Hi All

I have a ntext field that is databound to a Label control within a formview control. I am using an SqlDataSource control too.

What would be the best way to replace carriage returns (Chr(13) & Chr(10)) with <br /> tags.. I know it used to be the Replace() method.

Many thanks

Rit
__________________
Rit
www.designandonline.co.uk
INSPIRE | CREATE | DELIVER
 
Old March 15th, 2006, 03:06 PM
Imar's Avatar
Wrox Author
 
Join Date: Jun 2003
Posts: 17,089
Thanks: 80
Thanked 1,576 Times in 1,552 Posts
Default

Probably the easiest thing is to convert the field to a TemplateField.

Inside the template field use a custom function that replaces the value. The custom function can be placed in the COde Behind and could look like this:

Public Function ConvertBreaks(ByVal theValue As Object) As String
  Dim value As String = theValue.ToString()
  Return value.Replace(value, vbCrLf, "<br />")
End Function

HtH,

Imar
---------------------------------------
Imar Spaanjaars
Everyone is unique, except for me.
 
Old March 16th, 2006, 08:56 AM
Friend of Wrox
 
Join Date: Oct 2005
Posts: 173
Thanks: 0
Thanked 2 Times in 1 Post
Default

Hi Imar

Sorry I might have lead you a stray as I think you are talking about a Gridview field.

I am referring to a label control within a formview.

.. Anyway this seemd to do the job, can anyone see any problems that I could incur?...

Code:
<asp:Label ID="LabelField1" runat="server" 
Text='<%# Replace(Eval("Field1"), vbCrLf, "<br />") %>'></asp:Label>
Ta

Rit
 
Old March 16th, 2006, 11:56 AM
Imar's Avatar
Wrox Author
 
Join Date: Jun 2003
Posts: 17,089
Thanks: 80
Thanked 1,576 Times in 1,552 Posts
Default

Hi Rit,

If Field1 is a required field in the data source and always has a value, then this works fine.

However, I think it'll crash when the field is null / Nothing.

With the solution I posted earlier (a separate function that accepts an Object) you can check for nulls and act accordingly (e.g. return a "No Text" default text for example.

Cheers,

Imar---------------------------------------
Imar Spaanjaars
Everyone is unique, except for me.
 
Old March 17th, 2006, 05:05 AM
Friend of Wrox
 
Join Date: Oct 2005
Posts: 173
Thanks: 0
Thanked 2 Times in 1 Post
Default

Brilliant thanks Imar.

For some reason I am checking for nulls in my stored proc... is this bad practice in this situation?

Ta

Rit
 
Old March 18th, 2006, 04:24 AM
Imar's Avatar
Wrox Author
 
Join Date: Jun 2003
Posts: 17,089
Thanks: 80
Thanked 1,576 Times in 1,552 Posts
Default

In my opinion that's not necessarily a bad thing. Are you using COALESCE to change nulls into something else?

However, some of the ASP.NET controls have built-in support for translating nulls to a more useful text. If you change the nulls in the database to some "Not Found" text, you can no longer use that feature.

Imar
---------------------------------------
Imar Spaanjaars
Everyone is unique, except for me.
 
Old March 20th, 2006, 04:36 PM
Friend of Wrox
 
Join Date: Oct 2005
Posts: 173
Thanks: 0
Thanked 2 Times in 1 Post
Default

Imar, Thanks for the pointers.

In this situation the SQL solution seems to work fine. I'm not using COALESCE but rather IsNull()... Sounds lame but only because I have never used COALESCE.

Many thanks

Rit
 
Old March 21st, 2006, 03:33 AM
Imar's Avatar
Wrox Author
 
Join Date: Jun 2003
Posts: 17,089
Thanks: 80
Thanked 1,576 Times in 1,552 Posts
Default

There is nothing wrong with IsNull and it's certainly not lame (IMHO).

However, the good thing about COALESCE is that it allows you to enter multiple criteria (e.g. column names) and it will return the first non null value. This can be useful if you need to display some data, but you don't know which column has a non null value. So, it's related to IsNull, but not the same.....

Imar
---------------------------------------
Imar Spaanjaars
Everyone is unique, except for me.





Similar Threads
Thread Thread Starter Forum Replies Last Post
text-align:justify; and <BR> anshul HTML Code Clinic 23 June 18th, 2018 03:04 AM
Replace "<enter>" with "<br>" Varg_88 Classic ASP Basics 5 February 14th, 2011 12:33 PM
<br/> becomes <br></br> Kabe XSLT 1 July 6th, 2007 07:42 AM
to append <br> in XSLT dharus XSLT 4 August 1st, 2006 09:52 AM
Replace vbcrlf with <br> using C# kgriffin ASP.NET 1.0 and 1.1 Basics 2 May 4th, 2005 09:29 AM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.