ASP.NET <% %>的各种形式用法

1.<% %>用来绑定后台代码

< %
for(int i=0;i<10;i++)
{
Reaponse.Write(i.ToString());
}
%>

2、<%# %> 是在绑定控件DataBind()方法执行时被执行,用于数据绑定

<asp:Label ID="lblId" runat="server" Text='<%#Eval("title") %>' Visible="false"></asp:Label>

3、<%= %>是在客户端获取服务器端的变量,后台往前台传值

用来绑定后台的变量或方法且有返回值 的,但此时的变量名或方法的访问修饰符为protected或public

<%=name%> <%=getstr()%>

4、<%@ %>用来导入后台命名空间

<%@ import namespace="system.data">

5、<%$ %>用来绑定web.config里的字符串(键值对)

<asp:TextBox runat="server" ID="cc" Text="<%$ ConnectionStrings:pubs%>"></asp:TextBox>
web.config
<configuration>
<system.web>
<compilation debug="true" targetFramework="4.0" />
</system.web>
<connectionStrings>
<add name="pubs" connectionString="Server=.;database=pubs;uid=sa;pwd=" providerName="System.Data.SqlClient"/>
</connectionStrings>
</configuration>

6、<%: %>

这种方式是ASP.NET4.0中新加入的绑定方式,常用于MVC中,但普通webform中也可使用。功能其实就是对绑定的值进行一下编码,因此,一下两者是等同的:

<%= Server.HtmlEncode("<b>test</b>") %>
<%: "<b>test</b>" %>;
原文地址:https://www.cnblogs.com/yuan-jun/p/6784815.html