C# Tip - Rendering HTML in a Gridview cell

Posted by BobPalmer on Geeks with Blogs See other posts from Geeks with Blogs or by BobPalmer
Published on Sun, 07 Mar 2010 18:29:07 GMT Indexed on 2010/03/07 23:28 UTC
Read the original article Hit count: 299

Filed under:

Just a quick tip for working with the gridview in ASP.Net.  If your data column contains HTML text, you've probably seen something like this in your gridview after pulling the data:

<font color="red">First Item</font><br/><font color="green">Second Item</font><br/><font color="blue">Third Item</font>

To have the relevant column render in HTML, just go to your gridview property pages, find the column you need rendered in HTML, and click 'convert this Field into a TemplateField'.  The result is that as a template field, HTML within your bound data value will be rendered properly.  So our example above would transform into:

First Item
Second Item
Third Item

I primarily use this technique for enabling HTML content in comment fields, and to insert line breaks when building the data for these fields.

Hope this helps out!
-Bob
 

© Geeks with Blogs or respective owner