您的位置:首页 > 编程语言 > ASP

asp.net <%%>&<%#%>&<%=%>&<%@%>&<%$%>用法区别

2015-08-02 22:55 691 查看
在学习ASP.NET中经常会遇到<%%>等等,这样的东西,但是见到的多了有时候就混了。在网上发现一篇不错的文章,转过来,分享一下。

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

如:

< %

for(int i=0;i<100;i++)

{

Reaponse.Write(i.ToString());

}

%>

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

如: < %# Container.DataItem("title") %>

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.DataBind,获得的数据,系统会将其默认为String,怎样转化为其它的类型?

DataBinder.eval_r(Container.DataItem,"转换的类型","格式")  注:格式可以不要 此方法=eval_r("转换的类型")

转自:http://blog.sina.com.cn/s/blog_89cd68470101e3c8.html
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  asp.net