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?
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/>");
}
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.
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
Either replace line breaks \n
\r
\r\n
to <br/>
or use pre
tag instead of div.
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.
精彩评论