santhoshkumar B
santhoshkumar B

Reputation: 103

HTML Decoding of Eval database column value in repeater of ASP.NET webforms

I am trying to get HTML encoded value(processed and input by responsive Text Editor control) from database and decode it to display as normal plain text in the webform page.

I am able to get the values with Eval method in the repeater like below

<p><%#Eval("Content")%></p>

It works perfectly fine. I further tried to shorten it with following code

<%#Eval("Content").ToString().Length >= 170 ? Eval("Content").ToString().Substring(0, 170) : Eval("Content").ToString()%>

Does the needful, provided Database value is plain text and not HTML encoded one, which i have recently added, thus it stopped working.

Hence, i wish to know if I can use this following code to decode the encoded

<%=System.Web.HttpUtility.HtmlDecode()%>

I Have tried doing this but no joy, can someone help or correct me if the following code is wrong(i think)

<%=System.Web.HttpUtility.HtmlDecode(#Eval("Content"))%>

Upvotes: 0

Views: 2468

Answers (1)

Tom Troughton
Tom Troughton

Reputation: 4325

I believe you need:

<%# System.Web.HttpUtility.HtmlDecode(Eval("Content")) %>

The <%= is equivalent to Response.Write whereas <%# is data binding syntax which is what you need in this case. Your hash prefix on the Eval call is not valid syntax.

This is a useful reference to explain the difference between the various ASP.NET inline server tags: http://weblogs.asp.net/ahmedmoosa/embedded-code-and-inline-server-tags

Upvotes: 2

Related Questions