how to Embedded Code Blocks in ASP.NET Web Pages? - asp.net

where we use <%# %> <%= %> <%# %> etc.
what else asp tags can be added in asp.net web pages?

<%%> is short hand for:
<script runat="server">
</script>
Anyting inside the <% and %> is server side code.
The other variants are also shortcuts:
<%#%> is a page directrive
<%=%> is short for Response.Write
<%:%> is short for Response.Write, adding html encoding (introduced with .NET 4.0)
<%#%> is a binding expression
This page is a good reference to all these.

<%# %> - page directives, Register control
<%= %> - for the server code
<%# %> - for the eval kind of function its also for the server code

Related

Is there a way to translate web sites other than resx files?

I've been reading how to translate web sites either by using local or global resx files. Although this works great for buttons and small pieces of text in general, when I'm facing the translation of big chunks of text it seems kind of odd to me having to store long strings in the resx files, and spacing the text in small keys makes me unable to re-read what I just wrote.
For instance:
<p> <%$ Resources:MyResources, Welcome %> <%$ Resources:MyResources, to %> <%$ Resources:MyResources, my %> <%$ Resources:MyResources, wonderful %> <%$ Resources:MyResources, Website %> , <%$ Resources:MyResources, Where %> <%$ Resources:MyResources, you %> <%$ Resources:MyResources, can %> <%$ Resources:MyResources, Find %> <%$ Resources:MyResources, amazing %> <%$ Resources:MyResources, information %> <%$ Resources:MyResources, about %> <%$ Resources:MyResources, this %><%$ Resources:MyResources, and %><%$ Resources:MyResources, that %> <p>
Or:
<%$ Resources:MyResources, WelcomeToMyWonderfulWebsiteWhereYouCanFindAmazingInformationAboutThisAndThat %>
Is there a better way to handle multilingual support?
Thanks,
(forgot to add that I'm using VS2013 and this is for a C# WebForms site with aspx and ascx)
You can render directly from Resources, you don't to use the <%$ syntax. You can use <%= (or the auto-encoding <%: version instead):
In your case, I would have a single Resources.resx file in my project, with an entry for the Welcome Blurb with a short key:
Key: WelcomeMessage
Value (in `Resources.resx`): "Welcome to my website where..."
Value (in `Resources.de-DE.resx`): "Willkommen auf meiner Website..."
And in my .aspx files using <%:
<p><%: Resources.WelcomeMessage %>
Or using <%= if you want to render raw HTML:
<p><%= Resources.WelcomeMessage %>
Razor syntax:
<p>#Resources.WelcomeMessage</p>
Or for readability:
<p>#( Resources.WelcomeMessage )</p>
Resources is a static class that exists and automatically retrieves the correct resource value based on your current Thread culture value (so be sure you have code that correctly changes the Thread's culture), returning your fallback value if a translation doesn't apply.
You can view the original value in Visual Studio by hovering your mouse over each property value as the XML comments generated by the Resx designer include the first 100 or so characters from the resource string.

ASP.net: What is the difference between <%, <%= and <%#? [duplicate]

I have been using ASP.NET for years, but I can never remember when using the # and = are appropriate.
For example:
<%= Grid.ClientID %>
or
<%# Eval("FullName")%>
Can someone explain when each should be used so I can keep it straight in my mind? Is # only used in controls that support databinding?
There are a couple of different 'bee-stings':
<%# - page directive
<%$ - resource access
<%= - explicit output to page
<%# - data binding
<%-- - server side comment block
Also new in ASP.Net 4:
<%: - writes out to the page, but with HTML encoded
Also new in ASP.Net 4.5:
<%#: - HTML encoded data binding
<%= %> is the equivalent of doing Response.Write("") wherever you place it.
<%# %> is for Databinding and can only be used where databinding is supported (you can use these on the page-level outside a control if you call Page.DataBind() in your codebehind)
Databinding Expressions Overview
Here's a great blog post by Dan Crevier that walks through a test app he wrote to show the differences.
In essence:
The <%= expressions are evaluated at render time
The <%# expressions are evaluated at DataBind() time and are not evaluated at all if DataBind() is not called.
<%# expressions can be used as properties in server-side controls. <%= expressions cannot.

<%# %> vs <%= %> [duplicate]

This question already has answers here:
Closed 12 years ago.
Possible Duplicate:
ASP.NET “special” tags
What is the difference between <%# ... %>, <%= ... %> and <%$ ... %>?
I couldn't find anything information about this. It's impossible to find "<%=" using a search engine.
Do these tags have a name?
<%= ... %> is generally equivalent to Response.Write(...)
it cannot be used in a control attribute that is runat="server"
<%: ... %> (as of .NET v4.0) is an html encoded version of <%= %> (as #Eric mentions)
<%# ... %> is used in data-binding context for Bind, Eval or Output (as #Ray mentions)
<%$ ... %> is used in the context of a control attribute with runat="server" (google "expression builder" also have a look at making a general purpose 'Code' expression builder. it is evaluated when the attribute/Parameter is required by the control.
<%# %> will attempt to databind to a data source, using the Bind() function. This makes it a two-way function (read and write).
<%= %> will make the data read-only.
<%# %> is evaluated during data binding. It does not necessarily require Eval() or Bind() and Matthew suggested - I use it frequently to display plain text in a repeater control.
<%= %> is evaluated as the page renders. It is equivalent to calling Response.Write().
<%# %> can ONLY be used in data-binding context.
<%= %> expects a string value which it will then include in the output stream. So either a string variable or a method which returns a string. Anything else will cause an error.
I found some good information that clarifies the terminology for your future google searches:
http://authors.aspalliance.com/aspxtreme/aspnet/syntax/aspnetpagesyntax.aspx
Code Render Blocks:
<% inline code %>
A shortcut for HttpResponse.Write:
<%=inline expression %>
Data Binding Expressions:
<%# databinding expression %>
In a property:
<tagprefix:tagname property = "<%# databinding expression %>" runat="server" />
Server-side comments, such that they do not appear in the client's page source:
<%-- commented out code or content --%>

What is the difference between <%= %> syntax and <%# %> in asp.net?

What is the difference between <%= %> syntax and <%# %> in asp.net?
Here is a complete reference for all of these "inline tags," with links to MSDN for each type:
http://naspinski.net/post/inline-aspnet-tags-sorting-them-all-out-(3c25242c-3c253d2c-3c252c-3c252c-etc).aspx
<%= %> is basically used for string while <%# %> is used for data binding.
See following
http://support.microsoft.com/kb/307860
As compared to the <%= version, <%# is treated specially in parameters. It adds a data binding handler and sets the property at data bind time. That's why <%# does work in parameters and why it picks up the value at data bind time.

Tags in ASP.NET

What's the difference between <%= Something %> and <%# Something %>?
Are there any other modifiers of this kind?
Duplicate
What is the difference between the <%# and <%= opening tags?
See these questions:
What is the difference between the <%# and <%= opening tags?
In ASP.NET what are the differant ways to inline code in the .aspx?

Resources