<%# %>和<%= %>有什么區別?

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


Pardon my ASP ignorance, but what's the difference?

請原諒我對ASP的無知,但是有什么區別呢?

5 个解决方案

#1


18  

These are somewhat informally referred to as "bee stings". There are 4 types:

這些被非正式地稱為“蜜蜂螫刺”。有四類:

<%# %> is invoked during the DataBinding phase.

<%# %>在數據庫階段被調用。

<%= %> is used to get values from code to the UI layer. Meant for backward compatibility with ASP applications. Shouldn't use in .NET.

<%= %>用於從代碼獲取值到UI層。用於與ASP應用程序向后兼容。不應該在。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. The intention is for this to be the default usage (over %=) to help shield against script injection attacks.

<%: %>(在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 Web Forms頁面(。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. If you need...more explanation, please let me know.

@Esteban -添加msdn鏈接到指令。如果你需要…更多的解釋,請告訴我。

#2


3  

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

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

As Albert says, it's all to do with parsing databinding statements.

正如Albert所說,這一切都與解析數據庫語句有關。

#3


2  

The # version is used while data binding. <%= is just a simple Response.Write

在數據綁定時使用#版本。<%=只是一個簡單的Response.Write

#4


2  

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

#5


0  

javascript in .aspx that uses a master page.

在.aspx中使用母版頁的javascript。

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

注意!

本站翻译的文章,版权归属于本站,未经许可禁止转摘,转摘请注明本文地址:https://www.itdaan.com/blog/2008/10/01/759e3751c2d65f17144cd67b4c3a81e0.html



 
粤ICP备14056181号  © 2014-2021 ITdaan.com