惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

GbyAI
GbyAI
Cyberwarzone
Cyberwarzone
cs.AI updates on arXiv.org
cs.AI updates on arXiv.org
Threat Intelligence Blog | Flashpoint
Threat Intelligence Blog | Flashpoint
W
WeLiveSecurity
博客园 - 叶小钗
Hugging Face - Blog
Hugging Face - Blog
Security Latest
Security Latest
Scott Helme
Scott Helme
TaoSecurity Blog
TaoSecurity Blog
N
Netflix TechBlog - Medium
爱范儿
爱范儿
Application and Cybersecurity Blog
Application and Cybersecurity Blog
G
Google Developers Blog
F
Fortinet All Blogs
N
News and Events Feed by Topic
V2EX - 技术
V2EX - 技术
Google Online Security Blog
Google Online Security Blog
L
LINUX DO - 热门话题
NISL@THU
NISL@THU
The GitHub Blog
The GitHub Blog
Spread Privacy
Spread Privacy
S
Secure Thoughts
T
Tailwind CSS Blog
Google DeepMind News
Google DeepMind News
Recorded Future
Recorded Future
N
News and Events Feed by Topic
SecWiki News
SecWiki News
S
Security @ Cisco Blogs
A
About on SuperTechFans
云风的 BLOG
云风的 BLOG
L
Lohrmann on Cybersecurity
P
Palo Alto Networks Blog
Know Your Adversary
Know Your Adversary
IT之家
IT之家
人人都是产品经理
人人都是产品经理
Attack and Defense Labs
Attack and Defense Labs
Hacker News - Newest:
Hacker News - Newest: "LLM"
MyScale Blog
MyScale Blog
宝玉的分享
宝玉的分享
T
The Blog of Author Tim Ferriss
H
Hacker News: Front Page
T
Tenable Blog
C
CERT Recently Published Vulnerability Notes
D
DataBreaches.Net
阮一峰的网络日志
阮一峰的网络日志
Help Net Security
Help Net Security
博客园_首页
S
Securelist
罗磊的独立博客

博客园 - dyq

ASP.NET架构分析 ASP.NET 页面对象模型 基于aspnet Forms身份验证基本原理 - dyq - 博客园 实现简单的 Forms 身份验证 利用HttpModuler实现WEB程序同一时间只让一个用户实例登陆 ASP.NET 中处理页面“回退”的方法 建立一个使用.Net 2.0 MemberShip功能的标准例程(二)——配置篇 GridView 72般绝技 SQL Server 连接字符串和身份验证 .NET程序中常用的代码 - dyq - 博客园 分析ASP.NET服务器控件开发-控件生命周期 收藏网站制作常用经典ajax.prototype.javascript实例打包下载 .NET模板引擎 EFPlatform.CodeGenerator (代码生成器 静态页生成 生成HTML 模板) 深入浅出之正则表达式(一) 正则表达式学习 常用的正则表达式 ASP.NET URL Rewrite. URL重写 ASP.NET 生成静态页 .NET生成静态页面并分页
asp.net中<%%>形式的用法(原创)
dyq · 2008-01-24 · via 博客园 - dyq

今天第一天开通了博客,心情乐滋滋的,因为可以和园子里的朋友一起研究技术了。我希望把平时在项目中积累的知识以及自己学习的知识同园子里的朋友分享分享。为我们园子的壮大付出自己的一点努力。这是我发表的第二篇话题,希望对这<%%>语法不熟悉的朋友提供帮助,对已经熟悉的朋友,希望能提出你们宝贵的意见。
在asp.net中经常出现包含这种形式<%%>的html代码,我这里特别收集了,总的来说包含下面这样几种格式:
一.  <%%>
这种格式实际上就是和asp的用法一样的,只是asp中里面是vbscript或者javascript代码,而在asp.net中是.net平台下支持的语言。
特别注意:服务器控件中不能有<%%>语法
(这里我用C#代码)

  <%
        
int a = 2;
        
int b = 3;
        
int c = a + b;
        Response.Write(c);
    
%>

二. <%#%>
如果是这种格式的话那就是asp.net下特有的,它是控件数据绑定的语法,且必须要调用该控件的DataBind()方法才执行(或者整个页面Page.DataBind()也就是对所得控件都调用DataBind()方法)
特别注意:只有服务器控件才能用<%#%>语法

<div>
Server Control:
<asp:TextBox ID="TextBox1" runat="server" Text="<%#text%>"></asp:TextBox><br /><!--Server Control-->
Client Control:<input type="text" id="textbox2" value="<%#text%>" /><!--Client Control-->
 
</div>

aspx.cs代码如下:

  protected string text;//注意这里必须申明为public或protected,否则aspx页面(子类)无法访问
        protected void Page_Load(object sender, EventArgs e)
        
{
            
if (!Page.IsPostBack)
            
{
                
this.text = "aaaaaaaaaaaaa";
                
this.TextBox1.DataBind();//或this.DataBind();              
            }

        }

运行显示出来的效果

三.  <%=%>
这种形式实际上是由<%%>扩展而来的.等价于:Response.Write(<%%>)这种形式(不标准,但能看明白),它其实也可以看成是一种绑定.
aspx代码:

  <label id="label1"><%=DisplayStr()%></label><br />
  
<label id="label2" runat="server"><%=DisplayStr()%></label>

aspx.cs代码:

 public string  DisplayStr()//注意这里必须要有返回值,否则将会发生运行时错误
        {
            
return "bbbb";
        }

运行效果:

四.<%$%>
这种形式主要用于对web.config文件的键值对进行绑定:通常用于连接数据库的字符串
特别注意:1.绑定的只能是服务器控件
             2.只能绑定到服务器控件的某个属性上。

 <asp:TextBox runat="server" ID="cc" Text="<%$ConnectionStrings:pubs%>"></asp:TextBox>

web.config文件如下:

 <connectionStrings>
    
<add name="pubs" connectionString="Server=.;database=pubs;uid=sa;pwd=" providerName="System.Data.SqlClient"/>
  
</connectionStrings>

这样就能正常运行。
如果修改aspx中的文件:使用客户端的控件

 <input type="text" value="<%$ConnectionStrings:pubs%>"/>

或者:不绑定到服务器控件的某个属性上

<asp:TextBox runat="server" ID="cc"><%$ConnectionStrings:pubs%></asp:TextBox>

运行都会出现相同的错误

最后注意一点:
<%#%>只针对的是服务器控件的数据绑定,因此不能同<%=%>和<%%>混合使用

转自:

http://www.cnblogs.com/Charles2008/archive/2008/01/24/1051853.html