简体   繁体   English

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

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

请原谅我的ASP无知,但有什么区别?

These are somewhat informally referred to as "bee stings". 这些在某种程度上被非正式地称为“蜜蜂叮咬”。 There are 4 types: 有4种类型:

<%# %> is invoked during the DataBinding phase. 在DataBinding阶段调用<%# %>

<%= %> is used to get values from code to the UI layer. <%= %>用于从代码到UI层获取值。 Meant for backward compatibility with ASP applications. 意味着向后兼容ASP应用程序。 Shouldn't use in .NET. 不应该在.NET中使用。

<%@ %> represents directives and allow behaviors to be set without resorting to code. <%@ %>表示指令 ,允许在不诉诸代码的情况下设置行为。

<%: %> (introduced in ASP.NET 4) is the same as %= , but with the added functionality of HtmlEncoding the output. <%: %> (在ASP.NET 4中引入)与%=相同,但具有HtmlEncoding输出的附加功能。 The intention is for this to be the default usage (over %= ) to help shield against script injection attacks. 目的是将其作为默认用法(超过%= )以帮助防止脚本注入攻击。

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 Web窗体页(.aspx文件)和用户控件(.ascx)文件时页面和用户控件编译器使用的设置。

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). ASP.NET将任何不包含显式指令名称的指令块(<%@%>)视为@Page指令(对于页面)或作为@ Control指令(对于用户控件)。

@Esteban - Added a msdn link to directives. @Esteban - 添加了指令的msdn链接。 If you need...more explanation, please let me know. 如果您需要...更多解释,请告诉我。

The # version is used while data binding. 数据绑定时使用#版本。 <%= is just a simple Response.Write <%=只是一个简单的Response.Write

Not entirely related to the question, there's another related notation in asp.net called Expression Builder: 与问题不完全相关,在asp.net中有一个名为Expression Builder的另一个相关符号:

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

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

and it's extensible, see http://msdn.microsoft.com/en-us/magazine/cc163849.aspx#S4 并且它是可扩展的,请参阅http://msdn.microsoft.com/en-us/magazine/cc163849.aspx#S4

javascript in .aspx that uses a master page. .aspx中使用母版页的javascript。

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

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

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