tags:

views:

32

answers:

5

I have a list view that does something like this in the ItemTemplate:

<div><%# Eval("QualificationDescription") %></div>

My problem is that QualificationDescription has line breaks in it, if I put it in a TextBox it will display them but if I put it in a div it does not. Is there anyway to get the line breaks to show in a div?

A: 

Line breaks have to be rendered as <br/> tags within a div (the more sematic solution is to wrap all broken text fragments within <p> tags however.)

Your best bet is to come up with a simple ViewModel that will turn \n into <br/>, or go with the paragraph solution.

Pierreten
A: 

what i would do is where the DataBind() event is called then add an ItemDataBound and then create a new literal and then to the text of the literal add the QualificationDescription

then in the aspx page change div to be and then give the description.InnerText to be the literal

PaulStack
A: 

Either replace line breaks \n \r \r\n to <br/> or use pre tag instead of div.

Alex Reitbort
A: 

I agree with Pierreten, that a method to replace newlines with <br /> tags is a good option. One other possibility is to use a <pre> tag within the div. This will indicate that the browser should render the content as pre-formatted and display the linebreaks in the tag's content.

joe.liedtke
A: 

You could do this with the following code

  <div><%# AddLineBreaks(Eval("QualificationDescription")) %></div>

codebehind:

protected static string AddLineBreaks(object qualDescription) {
    if (qualDescription == null) {
        return "";
    }
    return qualDescription.ToString().Replace(Environment.NewLine, "<br/>");
}
Daniel Dyson