Page view counter

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

Last post 07-25-2008 11:33 AM by emrahustun. 10 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
    • Loading...
    • tod1d
    • Joined on 01-26-2006, 5:51 PM
    • Posts 17
    • Points 77

    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
    • Loading...
    • agolden
    • Joined on 08-03-2002, 6:56 AM
    • Houston, TX
    • Posts 1,007
    • Points 7,457

    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
    • Loading...
    • tod1d
    • Joined on 01-26-2006, 5:51 PM
    • Posts 17
    • Points 77

    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
    • Loading...
    • Sojan80
    • Joined on 10-28-2004, 7:59 AM
    • WI
    • Posts 185
    • Points 177
    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
    • Loading...
    • butterpecan
    • Joined on 06-22-2007, 6:37 PM
    • Posts 1
    • Points 2

    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
    • Loading...
    • emrahustun
    • Joined on 07-23-2008, 3:07 PM
    • Turkey
    • Posts 20
    • Points 24

    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
    • Loading...
    • emrahustun
    • Joined on 07-23-2008, 3:07 PM
    • Turkey
    • Posts 20
    • Points 24

    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
    • Loading...
    • conankingofcool
    • Joined on 12-13-2007, 3:43 PM
    • Houston/Dallas, TX
    • Posts 140
    • Points 645

     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
    • Loading...
    • emrahustun
    • Joined on 07-23-2008, 3:07 PM
    • Turkey
    • Posts 20
    • Points 24

    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
    • Loading...
    • agolden
    • Joined on 08-03-2002, 6:56 AM
    • Houston, TX
    • Posts 1,007
    • Points 7,457

    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
    • Loading...
    • emrahustun
    • Joined on 07-23-2008, 3:07 PM
    • Turkey
    • Posts 20
    • Points 24

     Thank you so much Contributor.

     That works.

    Emrah ÜSTÜN
Page 1 of 1 (11 items)