Posts
33
Comments
135
Trackbacks
0
Show line breaks in asp:label inside gridview

To show line breaks in asp:label element or for that matter inside Gridview, do the following  in case of Mandatory/ Nullable fields.

<ItemTemplate>  
       
<%# ((string)Eval("Details")).Replace("\n", "<br/>")
 
%>
 
</ItemTemplate>
 
 
 
<ItemTemplate>
 
       
<%# FormatString(Eval("Details"))
 
%>
 
</ItemTemplate>

 

In code behind, add the following FormatString function -

protected

string FormatString(string strHelpMessage)

{

string rtnString = string.Empty;

if (!string.IsNullOrEmpty(strHelpMessage))

rtnString = strHelpMessage.Replace(

Environment.NewLine, "<br/>");

return rtnString;

}

posted on Thursday, March 22, 2012 4:02 PM Print
Comments
Gravatar
# re: Show line breaks in asp:label inside gridview
Adam
3/22/2012 4:23 PM
<br /> tags inside of <label>..</label> is not valid html

labels are inline elements and should not have breaks or block elements nested inside them.
Gravatar
# re: Show line breaks in asp:label inside gridview
Vipin
3/22/2012 5:01 PM
@Adam, <asp:label> when displayed in HTML would be rendered as <span> elements and <label> element.

This is asp control <asp:label> element and not html <label> element. Hope this clarifies

Gravatar
# re: Show line breaks in asp:label inside gridview
Konsyantin
5/13/2015 2:05 PM
<ItemTemplate><%# FormatString(Convert.ToString(Eval("field_loss"))) %> </ItemTemplate>

Post Comment

Title *
Name *
Email
Comment *  
Verification