I have been using ASP.NET for years, but I can never remember when using the # and = are appropriate.
我已经使用ASP.NET多年了,但我永远不会记得使用#和=是否合适。
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?
有人可以解释每个人应该使用的时间,以便我能在脑海中保持直线吗? #仅用于支持数据绑定的控件吗?
3 个解决方案
#1
24
<%= %> is the equivalent of doing Response.Write("") wherever you place it.
<%=%>相当于在放置它的任何地方执行Response.Write(“”)。
<%# %> 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)
<%#%>用于数据绑定,只能在支持数据绑定的情况下使用(如果在代码隐藏中调用Page.DataBind(),则可以在控件外的页面级别使用这些)
Databinding Expressions Overview
数据绑定表达式概述
#2
41
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:
ASP.Net 4中的新功能:
-
<%:
- writes out to the page, but with HTML encoded - <%: - 写入页面,但使用HTML编码
#3
7
Here's a great blog post by Dan Crevier that walks through a test app he wrote to show the differences.
这是丹·克雷维尔(Dan Crevier)的一篇很棒的博客文章,它通过他编写的测试应用程序来展示差异。
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.
- <%#表达式在DataBind()时进行计算,如果未调用DataBind(),则根本不进行计算。
- <%# expressions can be used as properties in server-side controls. <%= expressions cannot.
- <%#表达式可用作服务器端控件中的属性。 <%=表达式不能。
#1
24
<%= %> is the equivalent of doing Response.Write("") wherever you place it.
<%=%>相当于在放置它的任何地方执行Response.Write(“”)。
<%# %> 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)
<%#%>用于数据绑定,只能在支持数据绑定的情况下使用(如果在代码隐藏中调用Page.DataBind(),则可以在控件外的页面级别使用这些)
Databinding Expressions Overview
数据绑定表达式概述
#2
41
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:
ASP.Net 4中的新功能:
-
<%:
- writes out to the page, but with HTML encoded - <%: - 写入页面,但使用HTML编码
#3
7
Here's a great blog post by Dan Crevier that walks through a test app he wrote to show the differences.
这是丹·克雷维尔(Dan Crevier)的一篇很棒的博客文章,它通过他编写的测试应用程序来展示差异。
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.
- <%#表达式在DataBind()时进行计算,如果未调用DataBind(),则根本不进行计算。
- <%# expressions can be used as properties in server-side controls. <%= expressions cannot.
- <%#表达式可用作服务器端控件中的属性。 <%=表达式不能。