List of what each of <% means (<%#, <%=, etc...) [duplicate] - c#

This question already has answers here:
Closed 12 years ago.
Possible Duplicate:
ASP.NET “special” tags
You know how you can embed property value from code-behind in your page by simply using <%= PropertyFromCodeBehind %> in your .aspx?
Well, I only recently discovered that and I can't seem to find any tutorial that would explain this (and related stuff) in more depth (I only know that <%# is used in conjuction with Eval) - probably because I'm using <% for searches.
So, can anybody provide me with more detail explanation of these tags, or give a link to some tutorial that explains all this? I'm interested in anything that can be learned on this subject; somewhere I saw that you can do fancy stuff like <% for ... %>.

Here is a good place to get started.
There are several different syntaxes:
<%$ %> Expression Syntax
<%# %> Data-Binding syntax
<% %> Evaluated Code Blocks
<%= %> Statement and Expression
New to ASP.NET 4 is the HTML encoding syntax (haacked). This is the same as <%= %> except the result is HTML encoded (for non IHtmlString types). The new syntax is intended to replace <%= %>.
<%: %> HTML Encoded output

See ScottGU's post post to get you started.

Related

Difference between special tags asp.net

I'm developing a front-end part of an application right now, and a question came to my mind.
What is the difference between asp.net special tags:
<%= %>
<%# %>
<%# %>
And if exists another special tag please describe its function.
<%= prints the raw value of the expression within.
This syntax can cause XSS vulnerabilities and should not be used.
<%: prints and HTML-escapes the value of the expression within.
<%# is like <%=, but is used for data-binding
<% executes a block of code and ignores and return values
<%# is used for directives like Page or Imports.
Check the below site Once..You will get an idea
http://naspinski.net/post/inline-aspnet-tags-sorting-them-all-out-(3c25242c-3c253d2c-3c252c-3c252c-etc).aspx
These are some useful special tags
<% %> 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
<%: %> Like <%= %> But HtmlEncodes the output (new with Asp.Net 4). 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
<%= %> Code Render Block - For evaluate inline expressions
<%# %> Directive Syntax - Usualy for linking the codebehind and a
asp.net page.
<%# %> Data binding
You can find more information at:
http://msdn.microsoft.com/en-us/library/fy30at8h(v=vs.85).aspx

ASP.NET <%# versus <% [duplicate]

This question already has answers here:
When should I use # and = in ASP.NET controls?
(3 answers)
In ASP.Net, what is the difference between <%= and <%# [duplicate]
(4 answers)
Closed 9 years ago.
I'm working on various asp.net pages .
For inline functions I do see 2 different formats are used:
Example 1:
<p><%Response.Write(now())%></p>
I also see another one with #:
Example 2:
<Asp:TextBox id="Textbox5" width="40" Text='<%# DataBinder.Eval(Container.DataItem, "Name") %>' runat="server" />
I want to know what is the exact different , <%# vs <%
Here is a good explanation here on stack -
In ASP.Net, what is the difference between <%= and <%# [duplicate]
Summary from those answers:
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 html-encodes the output.
The former is simply denotes some .NET code in the markup that outputs to the page.
The later uses Data Binding Expression Syntax to bind to a specific object.

How to mix embedded code blocks with data-binding expression

I read this document http://support.microsoft.com/kb/976112
where explains all the embedded code blocks available but I want to combine two of them.
I want to use base <% ... %> embedded code blocks with <%# ... %> data-binding expression
Example I want to add an "If" condition to this code:
<asp:Label ID="lblHello" runat="server" Text="<%# DataBinder.Eval(Container.DataItem, "[\"Txt\"]")%>"></asp:Label>
Regards.
The difference in the <% and <%# is mainly in when they're run (the former at render time, the latter at data binding). As such, it makes no sense to "combine" them.
What you likely want to do, is to run some additional code when data binding to do your if statement. If it's a simple expression, you can just inline it:
<%# MyProperty ? Eval("Txt") : Eval("OtherTxt") %>
If it's more complicated, then it's usually best to just call a code-behind method to do it for you:
<%# MyMethod(Eval("Txt")) %>

Difference between <%: %> and <%#: %> in Asp.Net

I know that we can <%: %> syntax for html encoding that is introduced in .Net 4. But I was reading new features of Asp.Net 4.5, and I got that we have another type i-e <%#: %> that is used for encoding the result of databind expression.
I am confuse with this.
What is the difference between <%: %> and <%#: %> in Asp.Net
Please explain both of them.
The same way that <%: %> is the HTML encoded version of <%= %>, the <%#: %> tag is the HTML encoded version of <%# %>.
The <%#: %> tag does the same as <%# %>, but then it calls Server.HTMLEncode on the string.
ASP.NET provides what's called a "binding" syntax to link HTML markup and controls to values extracted from data sources or other variables; that binding syntax is seen as something like:
<%# someVariable %>
The following colon merely extends the new "auto-HtmlEncode" behavior to the results of those bnding expressions.
Hope that helps.

Possible to do inline code in ASPX markup?

Is it possible to do things in a PHPish way in ASP.Net? I've seen <%= %> but I've tried it and couldn't get it to work.
The PHPish equivalent of what I want to do is
<script src="<?php echo ResolveUrl("jquery/js/jquery.js"); ?>"></script>
Yes, it's quite possible. You should familiarize yourself with all the variations of the (so called) alligator tags though.
Put code in between <% %> blocks. The <%= %> variant is a shortcut for Response.Write, and is used as a shortcut for directly outputting a variable to the page.
The following should work, as long as ResolveUrl is returning a string. Notice there is no ";" to end the line though.
<script src="<%= ResolveUrl("jquery/js/jquery.js") %>"></script>

Categories