简体   繁体   English

<%=%>和<%#%>之间有什么区别?

[英]What's the difference between <%= %> and <%# %>?

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. 现在我知道你可以在<%#%>之间添加一段代码,你必须调用Page.DataBind()方法来应用它,我认为这是<%#%>的工作原理。 But what does <%= %> mean? 但<%=%>是什么意思? When should I use it? 我应该什么时候使用它?

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. 希望这可以帮助。

  • <%= %> is used to reference properties/fields. <%= %>用于引用属性/字段。 It's like having a Response.Write "inlined" in the page at that position. 这就像在该位置的页面中有一个“内联”的Response.Write

  • <%# %> is used for data binding with Eval/Bind. <%# %>用于与Eval / Bind进行数据绑定。 Taken from MSDN 取自MSDN

The Eval method evaluates late-bound data expressions in the templates of data-bound controls such as the GridView, DetailsView, and FormView controls. Eval方法评估数据绑定控件(如GridView,DetailsView和FormView控件)模板中的后期绑定数据表达式。 At run time, the Eval method calls the Eval method of the DataBinder object, 在运行时,Eval方法调用DataBinder对象的Eval方法,

ASP.NET 4.0 introduces <%: something %> that is like <%= %> but escapes content (so it converts < to &lt; and so on) ASP.NET 4.0引入了<%: something %> ,它类似于<%= %>但是转义内容(因此它将<转换为&lt;等等)

So in the end you can use the <%# %> only in some controls (those that inherit from BaseDataBoundControl ) 所以最后你只能在一些控件中使用<%#%>(那些从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 (它不包含<%: %>

<%= ... %>

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: Displaying from ASP.NET 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标签......将它们全部排序

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM