I tried to find what the difference is between these two with Google, but I could not find an exact definition, and I could not exactly search for symbols either.
我试图找出这两者与谷歌之间的区别,但我找不到一个确切的定义,我也无法完全搜索符号。
Right now I know that you can put a piece of code between <%# %> and you have to call Page.DataBind() method to apply it, I think this is how <%# %> works. But what does <%= %> mean? When should I use it?
现在我知道你可以在<%#%>之间添加一段代码,你必须调用Page.DataBind()方法来应用它,我认为这是<%#%>的工作原理。但<%=%>是什么意思?我应该什么时候使用它?
3 个解决方案
#1
10
The Basic differences are:
基本差异是:
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 and are used to reference properties or fields.
<%=%>表达式不能用于引用属性或字段。
For example:
<%= Response.Write() %>
<ItemTemplate>
<%# DataBinder.Eval("Title") %>
</ItemTemplate>
You can have a more detailed explanation on msdn here: What's the difference between <%= %> and <%# %>
您可以在此处获得有关msdn的更详细说明:<%=%>和<%#%>之间的区别是什么
Hope this helps.
希望这可以帮助。
#2
4
-
<%= %>
is used to reference properties/fields. It's like having aResponse.Write
"inlined" in the page at that position.<%=%>用于引用属性/字段。这就像在该位置的页面中有一个“内联”的Response.Write。
-
<%# %>
is used for data binding with Eval/Bind. Taken from MSDN<%#%>用于与Eval / Bind进行数据绑定。取自MSDN
The Eval method evaluates late-bound data expressions in the templates of data-bound controls such as the GridView, DetailsView, and FormView controls. At run time, the Eval method calls the Eval method of the DataBinder object,
Eval方法评估数据绑定控件(如GridView,DetailsView和FormView控件)模板中的后期绑定数据表达式。在运行时,Eval方法调用DataBinder对象的Eval方法,
ASP.NET 4.0 introduces <%: something %>
that is like <%= %>
but escapes content (so it converts <
to <
and so on)
ASP.NET 4.0引入了<%:something%>,它类似于<%=%>但是转义内容(因此它将 <转换为<等等)< p>
So in the end you can use the <%# %> only in some controls (those that inherit from BaseDataBoundControl
)
所以最后你只能在一些控件中使用<%#%>(那些从BaseDataBoundControl继承的控件)
There is an article here http://msdn.microsoft.com/en-us/library/aa479321.aspx that explains how the data binding is done in .NET
这里有一篇文章http://msdn.microsoft.com/en-us/library/aa479321.aspx解释了如何在.NET中完成数据绑定
I'll add a link with a list of all the special inline tags of Asp.net: http://naspinski.net/post/inline-aspnet-tags-sorting-them-all-out-(3c25242c-3c253d2c-3c252c-3c252c-etc).aspx (it doesn't contain <%: %>
)
我将添加一个链接,其中列出了Asp.net的所有特殊内联标记:http://naspinski.net/post/inline-aspnet-tags-sorting-them-all-out-(3c25242c-3c253d2c-3c252c -3c252c-etc).aspx(它不包含<%:%>)
#3
1
<%= ... %>
Used for small chunks of information, usually from objects and single pieces of information like a single string or int variable:
用于小块信息,通常来自对象和单个信息,如单个字符串或int变量:
The Date is now <%= DateTime.Now.ToShortDateString() %>
The value of string1 is <%= string1 %>
MSDN:从ASP.NET显示
<%# .. %>
Used for Binding Expressions; such as Eval and Bind, most often found in data controls like GridView, Repeater, etc.:
用于绑定表达式;例如Eval和Bind,最常出现在GridView,Repeater等数据控件中:
<asp:Repeater ID="rptMeetings" DataSourceID="meetings" runat="server">
<ItemTemplate>
<%# Eval("MeetingName") %>
</ItemTemplate>
</asp:Repeater>
MSDN: Data-Binding Expressions Overview
MSDN:数据绑定表达式概述
Internet resource: Inline asp.net tags... sorting them all out
互联网资源:内联的asp.net标签......将它们全部排序
#1
10
The Basic differences are:
基本差异是:
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 and are used to reference properties or fields.
<%=%>表达式不能用于引用属性或字段。
For example:
<%= Response.Write() %>
<ItemTemplate>
<%# DataBinder.Eval("Title") %>
</ItemTemplate>
You can have a more detailed explanation on msdn here: What's the difference between <%= %> and <%# %>
您可以在此处获得有关msdn的更详细说明:<%=%>和<%#%>之间的区别是什么
Hope this helps.
希望这可以帮助。
#2
4
-
<%= %>
is used to reference properties/fields. It's like having aResponse.Write
"inlined" in the page at that position.<%=%>用于引用属性/字段。这就像在该位置的页面中有一个“内联”的Response.Write。
-
<%# %>
is used for data binding with Eval/Bind. Taken from MSDN<%#%>用于与Eval / Bind进行数据绑定。取自MSDN
The Eval method evaluates late-bound data expressions in the templates of data-bound controls such as the GridView, DetailsView, and FormView controls. At run time, the Eval method calls the Eval method of the DataBinder object,
Eval方法评估数据绑定控件(如GridView,DetailsView和FormView控件)模板中的后期绑定数据表达式。在运行时,Eval方法调用DataBinder对象的Eval方法,
ASP.NET 4.0 introduces <%: something %>
that is like <%= %>
but escapes content (so it converts <
to <
and so on)
ASP.NET 4.0引入了<%:something%>,它类似于<%=%>但是转义内容(因此它将 <转换为<等等)< p>
So in the end you can use the <%# %> only in some controls (those that inherit from BaseDataBoundControl
)
所以最后你只能在一些控件中使用<%#%>(那些从BaseDataBoundControl继承的控件)
There is an article here http://msdn.microsoft.com/en-us/library/aa479321.aspx that explains how the data binding is done in .NET
这里有一篇文章http://msdn.microsoft.com/en-us/library/aa479321.aspx解释了如何在.NET中完成数据绑定
I'll add a link with a list of all the special inline tags of Asp.net: http://naspinski.net/post/inline-aspnet-tags-sorting-them-all-out-(3c25242c-3c253d2c-3c252c-3c252c-etc).aspx (it doesn't contain <%: %>
)
我将添加一个链接,其中列出了Asp.net的所有特殊内联标记:http://naspinski.net/post/inline-aspnet-tags-sorting-them-all-out-(3c25242c-3c253d2c-3c252c -3c252c-etc).aspx(它不包含<%:%>)
#3
1
<%= ... %>
Used for small chunks of information, usually from objects and single pieces of information like a single string or int variable:
用于小块信息,通常来自对象和单个信息,如单个字符串或int变量:
The Date is now <%= DateTime.Now.ToShortDateString() %>
The value of string1 is <%= string1 %>
MSDN:从ASP.NET显示
<%# .. %>
Used for Binding Expressions; such as Eval and Bind, most often found in data controls like GridView, Repeater, etc.:
用于绑定表达式;例如Eval和Bind,最常出现在GridView,Repeater等数据控件中:
<asp:Repeater ID="rptMeetings" DataSourceID="meetings" runat="server">
<ItemTemplate>
<%# Eval("MeetingName") %>
</ItemTemplate>
</asp:Repeater>
MSDN: Data-Binding Expressions Overview
MSDN:数据绑定表达式概述
Internet resource: Inline asp.net tags... sorting them all out
互联网资源:内联的asp.net标签......将它们全部排序