Search code examples
c#asp.netteleriktelerik-grid

Allow HTML text in Radgrid template column


<ItemTemplate>
   <asp:Label ID="lblCompanyName" runat="server" 
      ToolTip='<%# CheckDbNull.ToStr(Eval("Name")) %>' 
      Text='<%# CheckDbNull.ToStr(Eval("Name")) %>'>
   </asp:Label>
</ItemTemplate>

I have some data in HTML formatted like <abc>, <Name>, etc., but it will render as HTML and it is not displayed.

How do I display HTML text in grid columns? I am using Telerik:RadGrid and template column.


Solution

  • use Server.HTMLEncode for displaying < and > sign.
    Follows

       Text='  <%# Server.HTMLEncode(CheckDbNull.ToStr(Eval("Name"))) %>'
    

    It changes as following

    • The less-than character (<) is converted to &lt ;.
    • The greater-than character (>) is converted to &gt ;.
    • The ampersand character (&) is converted to &amp ;.
    • The double-quote character (") is converted to &quot ;.
    • Any ASCII code character whose code is greater-than or equal to 0x80 is converted to &#< number>, where number is the ASCII character value.
      MSDN says Server.HTMLEncode

    Details are below
    http://msdn.microsoft.com/en-us/library/ms525347%28v=vs.90%29.aspx