<%: %> Syntax for HTML Encoding in a repeater

你说的曾经没有我的故事 提交于 2019-12-10 16:17:58

问题


Since .NET 4 its possible to use the <%: %> syntax for HTML Encoding of text.

In a repeater I use the following syntax to display data

<%# DataBinder.Eval(Container.DataItem, "fieldlabel")%>

The only way I know of to encode the output in the repeater is by using "Server.HtmlDecode". Is it possible to use the new <%: %> in a repeater just in combination with databinding so that I can remove the ugly HtlmDecode syntax. Or is an extention method my only option to improved the readability?


回答1:


No it is not possible. The <%# is allowing the evaulation of binding data but it use the basic <% block.

The only thing you can do is recreate the <%: by wrapping your code in Html.Encode.

Eg:

<%# Html.Encode(DataBinder.Eval(Container.DataItem, "fieldlabel")) %> 

The <%: is a shortcut and I guess not every variation of the use of the blocks has been captured to include a shortbut. MS probably didn't want to complicate the issue by creating a ton of different symbols to capture the various uses and only support the most common use.




回答2:


As of ASP.NET 4.5 this is possible using the new <%#: %> notation




回答3:


I think the answer is no, based on this question.

Meaning of the various symbols in .aspx page of asp.net




回答4:


It is possible but need to work more:

Please use below syntax

<asp:Literal ID="fieldlabel" runat="server" Mode="Encode" Text='<%#Eval("fieldlabel")%>"></asp:Literal>


来源:https://stackoverflow.com/questions/5869336/syntax-for-html-encoding-in-a-repeater

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!