Tuesday, September 19, 2006

 

Gridview: Convert /n line breaks to html br line breaks

It has been awhile since I have manipulated data in a Gridview. I am binding a 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:

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

I was wondering if there is an "easier"/"better" way of doing this, so I posted the question at the ASP.NET forums. Feel free to post your comments to that thread, rather than this blog entry.

Update

Aaron at ASP.NET provided me with a more aesthitic solution. See the code below:


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

Comments: Post a Comment

Links to this post:

Create a Link



<< Home
Content copyright ©2003-2006 Tod Birdsall