<%: %> Syntax for HTML Encoding in a repeater - asp.net

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?

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

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.

I think the answer is no, based on this question.
Meaning of the various symbols in .aspx page of asp.net

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>

Related

What are the difference between <%: and <%= in ASP .NET MVC 3

I'm just wondering, the only difference I know is that the <%= symbols generates any possible html tags that's included with the string your planning to display, while <%: just display what the string exactly look like. If anyone can help me with this, I will greatly appreciate it.
Pretty good explanation from Scott Gu - New <%: %> Syntax for HTML Encoding Output in ASP.NET 4 (and ASP.NET MVC 2)
Excerpt:
ASP.NET applications (especially those using ASP.NET MVC) often rely on using <%= %> code-nugget expressions to render output. Developers today often use the Server.HtmlEncode() or HttpUtility.Encode() helper methods within these expressions to HTML encode the output before it is rendered. This can be done using code like below:
<div>
<%= Server.HtmlEncode(Model.Content) %>
</div>
While this works fine, there are two downsides of it:
It is a little verbose
Developers often forget to call the Server.HtmlEncode method – and there is no easy way to verify its usage across an app
New <%: %> Code Nugget Syntax
With ASP.NET 4 we are introducing a new code expression syntax (<%: %>) that renders output like <%= %> blocks do – but which also automatically HTML encodes it before doing so. This eliminates the need to explicitly HTML encode content like we did in the example above. Instead, you can just write the more concise code below to accomplish the exact same thing:
<div>
<%: Model.Content %>
</div>
The two inline code tags are essentialy the same, the only difference being that <%: %> will automatically use encoding. So this:
<%: myText %>
is equivalent to this:
<%= Html.Encode(myText) %>
The former is recommended.
<%: is HtmlEncoded. Code Nuggets for asp.net
With ASP.NET 4 we are introducing a new code expression syntax (<%: %>) that renders output like <%= %> blocks do – but which also automatically HTML encodes it before doing so.
From Scott Gu blog:
With ASP.NET 4 we are introducing a new code expression syntax (<%:
%>) that renders output like <%= %> blocks do – but which also
automatically HTML encodes it before doing so. This eliminates the
need to explicitly HTML encode content like we did in the example
above. Instead, you can just write the more concise code below to
accomplish the exact same thing:
http://weblogs.asp.net/scottgu/archive/2010/04/06/new-lt-gt-syntax-for-html-encoding-output-in-asp-net-4-and-asp-net-mvc-2.aspx

ASP.NET Code Expression, Data Binding, and other Declarative Expressoins

What are the differences in these tags?
<%
<%#
<%=
<%$
More importantly, how do I display a page property using declarative syntax in an ASP.NET control? I'm trying to do this in an ASP.NET control. The task is to set the text of a label but I do not want to do this pro grammatically in the event I want to change the output control. I get an error about server side controls can't contain this syntax. I'm not sure that I need a databound control for what I want to do but that is another option.
Partial answer coming up.
Update
There is a new tag I've seen in ASP.NET 4.5? site
<%:
Partial answer
quoted from Mike Banavige
<% %> An embedded code block is
server code that executes during the
page's render phase. The code in the
block can execute programming
statements and call functions in the
current page class.
http://msdn2.microsoft.com/en-gb/library/ms178135(vs.80).aspx
<%= %> most useful for displaying
single pieces of information.
http://msdn2.microsoft.com/en-us/library/6dwsdcf5(VS.71).aspx
<%# %> Data Binding Expression Syntax.
http://msdn2.microsoft.com/en-us/library/bda9bbfx.aspx
<%$ %> ASP.NET Expression.
http://msdn2.microsoft.com/en-us/library/d5bd1tad.aspx
<%# %> Directive Syntax.
http://msdn2.microsoft.com/en-us/library/xz702w3e(VS.80).aspx
<%-- --%> Server-Side Comments.
http://msdn2.microsoft.com/en-US/library/4acf8afk.aspx
Update:
Okay this appears to work
<asp:Label ID="MyLabel" runat="server" Text='<%# MyProperty%>'></asp:Label>
If I use the eval syntax then I get an error about databound control or I use the <% then I get a server side controls error. Any more color appreciated.. not sure I really understand what is going on.
Perhaps it has something to do with the render phase.
Few more observations:
I can use <%= without databinding and get the property value but can not use it in a server side control without getting error.
If I use <%# in server side control but I'm required to do a Page.Databind.
Interestingly, I can use either <%= or <%# when I want to render text that is not inside a control. Although the latter requires databinding.
The new <%: syntax is explained, also called code expression syntax
With ASP.NET 4 we are introducing a new code expression syntax (<%:
%>) that renders output like <%= %> blocks do – but which also
automatically HTML encodes it before doing so.
http://weblogs.asp.net/scottgu/new-lt-gt-syntax-for-html-encoding-output-in-asp-net-4-and-asp-net-mvc-2
No, server-side controls can't. For example, I have a string property named SkinPath that give me the full App_Themes path to the current theme. I use it in the following way:
<img src='<%= SkinPath %>/Images/myImage.png' />
However, the following doesn't work:
<asp:Image ID='image' runat='server' ImageUrl='<%= SkinPath %>/Images/myImage.png' />
Instead, it renders the src literally in the result <img>.

Inline scripting in ASP.NET

I want to learn advanced and basic things about ASP.NET inline scripting like
<img src="<%= Page.ResolveUrl("~")%>Images/Logo.gif"/>
or
<asp:Label ID="lblDesc" runat="server" Text='<%#DataBinder.Eval(Container.DataItem,"Description")%>'></asp:Label>
And so on...
And, what's the difference between <% %> and <%# %> and such stuff?
Where can I find from basic to advanced implementation of those usages?
Check out this article for the specifics of the different inline tag options.
From the article:
<% ... %> -- The most basic inline tag,
basically runs normal code:
<%= ... %> -- Used for small chunks of
information, usually from objects and
single pieces of information
like a single string or int variable:
<%# ... %> -- Used for Binding Expressions;
such as Eval and Bind, most often
found in data controls like GridView,
Repeater, etc.:
<%$ ... %> -- Used for expressions, not code;
often seen with DataSources:
<%# ... %> -- This is for directive syntax;
basically the stuff you see at the top
your your aspx pages like control
registration and page declaration:
<%-- ... %> -- This is a server side comment,
stuff you don't want anyone without
code access to see:
In general, <%#..%> is used for preprocessing a template, such as when databinding, whereby the names of properties of the objects are not known at compile-time. If, for example, you have an ASP.NET Repeater object, and you databind a list of objects to it, this notation is used to pre-populate values that could not be set at any point except during the databind lifecycle.
The other notations, <%..%> and <%=..%> are more standard and you'll see these far more often than the other syntax previously discussed, especially if you use something like ASP.NET MVC instead of ASP.NET Web Forms. The syntax <%..%> executes arbitrary script inline, and nothing more, but allows you to write entire blocks of .NET code such as if statements, while loops, for loops, etc. The syntax <%=..%> is an evaluate-and-write syntax, and is rough equivalent of <% Response.Write([..].ToString()) %>. I.e., <%= myVal %> is the same as <% Response.Write(myVal.ToString()) %>
These syntaxes are basic ASP.NET knowledge.

Asp.Net - what is <%$?

I should know this by now, but I don't, and for some reason, I am not finding the answer on Google, so I thought I'd try here.
I know that <%= %> is the equivalent of Response.Write()
And I've seen <%# %> for databinding.
However, today I noticed something new, and even though I can see what it's doing, I am looking for the official documentation on this. In one of my web pages, I see
ConnectionString="<%$ ConnectionStrings:SomeConnectionString %>"
So what does <%$ %> do?
See this question:
In ASP.Net, what is the difference between <%= and <%#
In summary ,there are a several different 'bee-stings':
<%# - Page/Control/Import/Register directive
<%$ - Resource access and Expression building
<%= - Explicit output to page, equivalent to <% Response.Write( ) %>
<%# - Data Binding. It can only used where databinding is supported, or at the page level if you call Page.DataBind() in your code-behind.
<%-- - Server-side comment block
<%: - Equivalent to <%=, but it also HTMLEncode()s the output.
Used for expressions, not code; often seen with DataSources
http://msdn.microsoft.com/en-us/library/d5bd1tad.aspx
It's markup used to evaluate expressions rather than code.

What are these called <% ... %>? And what ways can they be used in a page

In a .Net web form...
What are these special tags called?
I know of 2: <%-- comment --%> for comments
and <%# used with Eval and Bind #>
So what does the # signify? Are there more?
I know you can put some basic logic and function calls in there but I've never found anything that really describes how that can be used and should be used.
I hope this isn't a duplicate but it's really hard to search for <%#
They're also called bee-stings:
In ASP.Net, what is the difference between <%= and <%#
Server tags. They are called server tags.
Of course there are more.
<%= "string constant" %> - it will output a given string to the HTML output
<%= BO.Customer.GetName () %> - will do the same with the function that returns a string result
<% RenderMyCoolControl %> - without the "=" character, it is supposed that your function will render something to the HTML output using Response.Write
Or you can use that directly: <% Response.Write ("string constant") %>
Hope that helps.
Used for Binding Expressions; such as Eval and Bind, most often found in data controls like GridView, Repeater, etc
Are there more?
Take a look here:
Inline tags
There's a fairly complete list here:
ASP.NET "special" tags
And as the accepted answer on there states, they are "Server Side Scripting Tags"

Resources