Replacing \n line breaks with HTML br line breaks in Gridview; what is the recommended practice.

Last post 07-16-2009 5:52 AM by crossbyname. 11 replies.

Sort Posts:

  • Replacing \n line breaks with HTML br line breaks in Gridview; what is the recommended practice.

    09-19-2006, 11:45 AM
    • Member
      79 point Member
    • tod1d
    • Member since 01-26-2006, 5:51 PM
    • Posts 19

    Hello,

    It has been awhile since I have manipulated data in a Gridview. I am binding an collection to a Gridview and one of the values contains non-html line breaks (\n). I would like to convert these to html line breaks (<br />). The code below works fine.

    protected void gridHistory_RowDataBound(object sender, GridViewRowEventArgs e)
    {
    	GridViewRow row = e.Row;
    	if (e.Row.RowType == DataControlRowType.DataRow)
    	{
    		row.Cells[0].Text = row.Cells[0].Text.Replace("\n", "<br />");
    	}
    }

    Is there an "easier"/"better" way of doing it?

    Thanks.

    Tod Birdsall, MCSD for .NET
    blog: http://tod1d.net
    Filed under:
  • Re: Replacing \n line breaks with HTML br line breaks in Gridview; what is the recommended practice.

    09-19-2006, 11:46 PM
    Answer
    • Star
      7,657 point Star
    • agolden
    • Member since 08-03-2002, 10:56 AM
    • Houston, TX
    • Posts 1,032

    Todd,

    I typically put that type of formatting in the GridView markup, either explicitly if the field is never null, or calling a method to handle the null values and make the replacement.

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

    Aaron 

    Don't forget to click "Mark as Answer" on the post that helped you. This credits that member, earns you a point and marks your thread as Resolved so everyone will know you have been helped.
  • Re: Replacing \n line breaks with HTML br line breaks in Gridview; what is the recommended practice.

    09-20-2006, 10:00 AM
    • Member
      79 point Member
    • tod1d
    • Member since 01-26-2006, 5:51 PM
    • Posts 19

    Hi Aaron,

    Your solution is more aesthtic than mine and it works just as well.

    Thank you for the input.

    Tod Birdsall, MCSD for .NET
    blog: http://tod1d.net
  • Re: Replacing \n line breaks with HTML br line breaks in Gridview; what is the recommended practice.

    11-21-2006, 10:17 AM
    • Member
      181 point Member
    • Sojan80
    • Member since 10-28-2004, 7:59 AM
    • WI
    • Posts 190
    agolden this solution just rocks! YOu are indeed a sanity saver!
  • Re: Replacing \n line breaks with HTML br line breaks in Gridview; what is the recommended practice.

    07-17-2007, 6:09 PM
    • Member
      2 point Member
    • butterpecan
    • Member since 06-22-2007, 6:37 PM
    • Posts 1

    oh my god, you guys are awesome!!

    I have tried this problem for two days, but I couldn't figure it out.

    Finally, I got it!!

    Thank you Aaron

  • Re: Replacing \n line breaks with HTML br line breaks in Gridview; what is the recommended practice.

    07-23-2008, 3:15 PM
    • Member
      24 point Member
    • emrahustun
    • Member since 07-23-2008, 7:07 PM
    • Turkey
    • Posts 20

    After 6 years,

    thank you for solution. i was searching everywhere...

    Emrah ÜSTÜN
  • Re: Replacing \n line breaks with HTML br line breaks in Gridview; what is the recommended practice.

    07-24-2008, 3:16 PM
    • Member
      24 point Member
    • emrahustun
    • Member since 07-23-2008, 7:07 PM
    • Turkey
    • Posts 20

    Hello,

    i'm beginner for asp.net.

    now i have a problem with this.

    <asp:Label ID="Label2" runat="server" Text='<%# ((string)Eval("about")).Replace("\n", "<br/>")  %>'></asp:Label>
    if about is empty, it's giving an error. how can i use if(Eval("about"))!=null) whit this?
    Thanks. 
    Emrah ÜSTÜN
  • Re: Replacing \n line breaks with HTML br line breaks in Gridview; what is the recommended practice.

    07-24-2008, 4:42 PM
    • Member
      657 point Member
    • conankingofcool
    • Member since 12-13-2007, 3:43 PM
    • Houston/Dallas, TX
    • Posts 141

     You can make your own Server-Side function that takes a string as the parameter and does the replace only if the string isn't null and then returns it. Then you can do this.

    <asp:Label ID="Label2" runat="server" Text='<%# YourFunction(Eval("about")) %>'></asp:Label>

    We're all in this together.
  • Re: Replacing \n line breaks with HTML br line breaks in Gridview; what is the recommended practice.

    07-24-2008, 6:03 PM
    • Member
      24 point Member
    • emrahustun
    • Member since 07-23-2008, 7:07 PM
    • Turkey
    • Posts 20

    thank you very much.
    actually, i tried it before.
    if "about" is empty, i thing eval is giving this error. (with or without an if-null function)

    so this didn't work too: satirdonusum((string)Eval("about"))

    Error is:
    Unable to cast object of type 'System.DBNull' to type 'System.String'.

    i can't fix this. can you help?

    Emrah ÜSTÜN
  • Re: Replacing \n line breaks with HTML br line breaks in Gridview; what is the recommended practice.

    07-24-2008, 8:46 PM
    • Star
      7,657 point Star
    • agolden
    • Member since 08-03-2002, 10:56 AM
    • Houston, TX
    • Posts 1,032

    Hi emrahustun,

    A way to work around this is to declare your formatting method to accept an object, then check for DBNull and format appropriately.  So, you'd have:

    <%# Eval("about") %>

    and the formatting method would be something like:

    protected string FormatString(object o)
    {
        if (o.Equals(DBNull.Value))
        {
            return String.Empty;
        }
        else
        {
            return ((string)o).Replace("\n", "&lt;br/&gt;");
        }
    }

    Hope that helps.

    Aaron

    Don't forget to click "Mark as Answer" on the post that helped you. This credits that member, earns you a point and marks your thread as Resolved so everyone will know you have been helped.
  • Re: Replacing \n line breaks with HTML br line breaks in Gridview; what is the recommended practice.

    07-25-2008, 11:33 AM
    • Member
      24 point Member
    • emrahustun
    • Member since 07-23-2008, 7:07 PM
    • Turkey
    • Posts 20

     Thank you so much Contributor.

     That works.

    Emrah ÜSTÜN
  • Re: Replacing \n line breaks with HTML br line breaks in Gridview; what is the recommended practice.

    07-16-2009, 5:52 AM
    • Member
      6 point Member
    • crossbyname
    • Member since 06-09-2008, 8:56 AM
    • UK
    • Posts 23

     Hi, this alternative may be useful for any VB user formating in a DetailsView:

    <asp:TemplateField HeaderText="Description">
                    <ItemTemplate>
                     <%#Replace(Container.DataItem("Description"), vbCrLf, "<br>")%>
                    </ItemTemplate>
                    </asp:TemplateField>


    Thank you Aaron for the starter, helped me work this out.

    Tim

Page 1 of 1 (12 items)