Reputation: 81771
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?
Upvotes: 2
Views: 3222
Reputation: 6763
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.
Upvotes: 2
Reputation: 27581
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:
Upvotes: 10