开发者

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

开发者 https://www.devze.com 2022-12-27 06:57 出处:网络
This question already has answers here: Closed 12 years ago. Possible Duplicate: ASP.NET “special” tags
This question already has answers here: Closed 12 years ago.

Possible Duplicate:

ASP.NET “special” tags

What is the difference between <%#开发者_StackOverflow ... %>, <%= ... %> 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 --%>
0

精彩评论

暂无评论...
验证码 换一张
取 消