I wanted a clean and simple way to HtmlEncode these so they would render correctly. I first tried to make the Server.HtmlEncode method work inline but I didn't have much luck. Quite a few search results on Google said it was not possible and most people suggested encoding the text inside a DataBound event.
I eventually found the answer to doing it inline, which I think is much cleaner. My original markup was:
<%# DataBinder.Eval(Container.DataItem, "FirstName") %>
To get this encoded I use:
<%#Server.HtmlEncode((string)DataBinder.Eval(Container.DataItem, "FirstName"))%>
I tried many variations on this but it was the cast to string that I was missing It's not immediately obvious that cast is required as the documentation for Eval suggests a string is returned.
No comments:
Post a Comment