Last post Apr 11, 2005 05:45 PM by hardeyk
Apr 11, 2005 02:51 PM|hardeyk|LINK
I'm looking for the best way of replacing line feeds in my output.. I think I'm close(ish) and have seen a suggestion elsewhere.. but haven't got there yet.
What I have is this.. <textaera> input into a DB (where it is stored correctly on new lines as required).
A datalist is created to show the output.. In classic ASP I would loop through the code and do a replace(string, chr(10)&chr(13), "<br>") to get the correct output.
However with .net I cannot affect the #databinder directly..
The best suggestion I had seen was to create a OnItemDataBound funtion, which would then do a Regular expression change to the displayed text.
However, I'm struggling to find all the correct Object names etc to use..
Here's my current C# code.
private void DisplayItems_ItemCreated(object sender, DataListItemEventArgs e)
case ListItemType.Item :
e.Item.DataItem = Regex.Replace(e.Item.DataItem.ToString(), @"\n\r", "<br>");
I know that as far as the case works OK because I've added an alert and it pops up the required number of times.
The areas I think I have issues with are:
Identifying the correct DataItem in the datalist (I've tried using .name but this isn't correct)
Writing the text back.. I know e.Item.DataItem is wrong because if I just use that and try - e.Item.DataItem = "X"; - nothing gets changed.
the "\n\r" - are these really created when the ToString() is used?
Hope this is enough info.
Could any answers please be in as near to English and not Techno speak as possible.
Apr 11, 2005 04:29 PM|dpl|LINK
Another approach would be to use template columns and helper functions (You can bind your control to a server-side function which serves as a middle tier for processing the bound data)
Here is a good article: http://aspalliance.com/31
You can create a function "ReplaceLineBreaks which accepts a string input and replaces the line breaks in the input parameter using your Regex logic, and returns the result string.
Apr 11, 2005 05:45 PM|hardeyk|LINK
My code is now much simpler and I have the final issue resolved.
For those interested here is the final code
private String RemoveCRLF(String strToChange)
return Regex.Replace(strToChange, "\r\n", "<br>");
<td><%# RemoveCRLF(DataBinder.Eval(Container.DataItem, "detail").ToString()) %></td>