views:

164

answers:

2

Accidentally I found this post about a new feature in ASP.NET 4.0: Expressions enclosed in these new brackets <%: Content %> should be rendered as HTML encoded.

I've tried this within a databound label in a FormView like so:

<asp:Label ID="MyLabel" runat="server" Text='<%: Eval("MyTextProperty") %>' />

But it doesn't work: The text property contains script tags (for testing), but the output is blank. Using the traditional way works:

<asp:Label ID="MyLabel" runat="server"
    Text='<%# HttpUtility.HtmlEncode(Eval("MyTextProperty")) %>' />

What am I doing wrong?

(On a sidenote: I am too stupid to find any information: Google refuses to search for that thing. The VS2010 Online help on MSDN offers a lot of hits, but nothing related to my search. Stackoverflow search too. And I don't know how these "things" (the brackets I mean) are officially called to have a better search term.)

Any info and additional links and resources are welcome!

Thanks in advance!

+5  A: 

You are confusing data binding expressions, which have the syntax <%#%> and are used with Eval (and Bind) with the response output tags (<%=%> and <%:%>) that cannot be used with Eval.

Oded
Thanks, that's the point. So: back to HttpUtility.HtmlEncode... sigh...
Slauma
+1  A: 

Sanity check 1: when you do a "view source" of the rendered page, do you see that <%: %> markup? (you shouldn't)

Sanity check 2: is your site really using asp.net 4.0? This feature doesn't work in 2.0.

Hans Kesting
"Yes" and "Yes". I think, Oded is right: when I use the brackets outside of the FormView and without Eval it works.
Slauma