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

时间:2020-03-06 15:00:06  来源:igfitidea点击:

请原谅我对ASP的了解,但是有什么区别?

解决方案

数据绑定时使用该版本。 <%=只是一个简单的Response.Write

参见http://weblogs.asp.net/leftslipper/archive/2007/06/29/how-asp-net-databinding-deals-with-eval-and-bind-statements.aspx

正如Albert所说,这与解析数据绑定语句有关。

这些在某种程度上被非正式地称为"蜜蜂st"。有四种类型:

在数据绑定阶段调用<%%>。

<%=%>用于从代码获取值到UI层。旨在与ASP应用程序向后兼容。不应在.NET中使用。

" <%@%>"代表指令,并允许在不借助代码的情况下设置行为。

" <%:%>"(在ASP.NET 4中引入)与"%="相同,但具有HtmlEncoding输出的添加功能。目的是将其用作默认用法(超过%%),以帮助抵御脚本注入攻击。

Directives specify settings that are
  used by the page and user-control
  compilers when the compilers process
  ASP.NET Web Forms pages (.aspx files)
  and user control (.ascx) files.
  
  ASP.NET treats any directive block
  (<%@ %>) that does not contain an
  explicit directive name as an @ Page
  directive (for a page) or as an @
  Control directive (for a user
  control).

@Esteban在指令中添加了msdn链接。如果我们需要...更多说明,请告诉我。

与问题不完全相关,asp.net中还有另一个相关的表示法,称为Expression Builder:

<asp:SqlDataSource ... Runat="server"
 ConnectionString="<%$ ConnectionStrings:Northwind %>"
/>

<asp:Literal Runat="server"
  Text="<%$ Resources:MyResources, MyText %>"
/>

而且它是可扩展的,请参见http://msdn.microsoft.com/zh-cn/magazine/cc163849.aspx#S4

使用母版页的.aspx中的javascript。

var e = document.getElementById('<%= lblDescription.ClientID %>');
e.innerHTML = 'getElementById(\'lblDescription\') will be null';