简体   繁体   中英

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"))%>

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM