关于<%= %>

      ASP和ASP.NET中都有"<%=%>",用法也一样,但在ASP.NET中就是会出错.先看一段ASP的代码
< %
Function  DisplayStr()
    '....'

    Response.Write 
" 字符串 "
End Function
%
>
< div >< % =  DisplayStr()% ></ div >
这样的代码在ASP中应该很常见,但把上面的代码改成ASP.NET的代码,用同样的方式调用的时候就会报错.
< script runat = " server " >
void  DisplayStr()
{
    Response.Write(
"字符串");
}

< script >
< div ><%=  DisplayStr() %>< div >
原因是因为在ASP.NET中使用<%=%>就等于使用 HtmlTextWriter.Write方法,此方法的参数不允许以方法(函数)作为参数.这可能就是强类型语言和弱类型语言的差别吧.8-),这里要想让程序运行真确可以让Display()返回一个字符串.我不知道在ASP中"<%=%>"被编译成了什么,有知道的朋友可以告诉我一下.

看了大家的回复,觉得自己写的文章缺少可信度,所以决定把如何证明<%= %>就等于HtmlTextWrite.Write的过程写出来
<% @ Page Language = " C# "  AutoEventWireup = " true "   CodeFile = " Default.aspx.cs "  Inherits = " _Default "   %>

<! DOCTYPE html PUBLIC  " -//W3C//DTD XHTML 1.0 Transitional//EN "   " http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd " >

< script language = " c# "  type = " text/C# "  runat  = " server "   >
        
public   string  DisplayStr()
       
{
           
return "显示字符串!";
       }
 
</ script >
< html xmlns = " http://www.w3.org/1999/xhtml "   >
< head runat = " server " >
    
< title > 无标题页 </ title >
</ head >
< body >
    
< form id = " form1 "  runat = " server " >
    
< div ><%=   " 显示字符串! " %>   </ div >
    
< div ><%=  DisplayStr()  %>   </ div >
    
</ form >
</ body >
</ html >
上面是一个完整的页面,我们可以用 Reflector反编译一下他生产的代码得到下面的结果

大家把红线圈起来的部分和源代码对比一下就能知道<%= %>表示什么了.

补充:<%= %>不能用在服务器控件的属性里面
1. < asp:Label ID = " Label3 "  runat = " server "  Text = " <%=DisplayStr() %> " ></ asp:Label >
2.<asp:Label ID="Label3" runat="server" Text="<%="文本框"%>"></asp:Label>  
3.  <asp:Label ID="Label3" runat="server" Text="田田田"><%=DisplayStr()%></asp:Label>
上面的第一种用法不会报错但,但得不到期望的值,会返回如下的结果.
< span  id ="Label3" > <% = DisplayStr() %> </ span >
第二种用法会报错.
第三种用法是没有问题的,不过<%=DisplayStr()%>的值会覆盖Text="田田田",原因是因为编译的顺序不同,但Label3的Text的值任然是"田田田"

题外话:在ASP.NET中已经很少看到<%= %>了,因为有了新的替代产品<%# %>,看上去有几分相似,但在使用上比<%=%>方便,而且也更强大.具体的可以参考数据绑定相关的内容.

转载于:https://www.cnblogs.com/Lyhik/archive/2008/01/23/1050462.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
帮我补充这段代码<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>外卖订餐网站首页</title> <link rel="stylesheet" href="css/style.css"> </head> <body> <header> <div class="logo"> <img src="logo.png" alt="网站Logo"> </div> <nav> <ul> <li><a href="#">首页</a></li> <li><a href="#">菜单</a></li> <li><a href="#">关于我们</a></li> <li><a href="#">联系我们</a></li> </ul> </nav> </header> <div class="banner"> <img src="banner.jpg" alt="轮播图"> </div> <section class="featured-dishes"> <h2>推荐菜品</h2> <ul> <li> <img src="dish1.jpg" alt="菜品图片"> <h3>菜品名称</h3> <p>菜品描述</p> <span class="price">¥50</span> </li> <li> <img src="dish2.jpg" alt="菜品图片"> <h3>菜品名称</h3> <p>菜品描述</p> <span class="price">¥60</span> </li> <li> <img src="dish3.jpg" alt="菜品图片"> <h3>菜品名称</h3> <p>菜品描述</p> <span class="price">¥70</span> </li> </ul> </section> <section class="order-form"> <h2>下单</h2> <form> <label for="name">姓名</label> <input type="text" id="name"> <label for="phone">电话</label> <input type="tel" id="phone"> <label for="address">地址</label> <input type="text" id="address"> <label for="dish">菜品</label> <select id="dish"> <option value="1">菜品1</option> <option value="2">菜品2</option> <option value="3">菜品3</option> </select> <button type="submit">提交订单</button> </form> </section> <footer> <p>©2021 外卖订餐网站. All Rights Reserved.</p> </footer> </body> </html>添加左边导航栏和广告浮动窗口,请给我html和css的源代码
06-02
以下是添加左边导航栏和广告浮动窗口的 HTML 和 CSS 源代码: HTML 代码: <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>外卖订餐网站首页</title> <link rel="stylesheet" href="css/style.css"> </head> <body> <header> <div class="logo"> <img src="logo.png" alt="网站Logo"> </div> <nav> <ul> <li><a href="#">首页</a></li> <li><a href="#">菜单</a></li> <li><a href="#">关于我们</a></li> <li><a href="#">联系我们</a></li> </ul> </nav> <div class="left-nav"> <ul> <li><a href="#">热销</a></li> <li><a href="#">折扣</a></li> <li><a href="#">新品</a></li> </ul> </div> </header> <div class="banner"> <img src="banner.jpg" alt="轮播图"> </div> <section class="featured-dishes"> <h2>推荐菜品</h2> <ul> <li> <img src="dish1.jpg" alt="菜品图片"> <h3>菜品名称</h3> <p>菜品描述</p> <span class="price">¥50</span> </li> <li> <img src="dish2.jpg" alt="菜品图片"> <h3>菜品名称</h3> <p>菜品描述</p> <span class="price">¥60</span> </li> <li> <img src="dish3.jpg" alt="菜品图片"> <h3>菜品名称</h3> <p>菜品描述</p> <span class="price">¥70</span> </li> </ul> </section> <section class="order-form"> <h2>下单</h2> <form> <label for="name">姓名</label> <input type="text" id="name"> <label for="phone">电话</label> <input type="tel" id="phone"> <label for="address">地址</label> <input type="text" id="address"> <label for="dish">菜品</label> <select id="dish"> <option value="1">菜品1</option> <option value="2">菜品2</option> <option value="3">菜品3</option> </select> <button type="submit">提交订单</button> </form> </section> <div class="ad"> <img src="ad.jpg" alt="广告图片"> </div> <footer> <p>©2021 外卖订餐网站. All Rights Reserved.</p> </footer> </body> </html> CSS 代码: /* 左边导航栏样式 */ .left-nav { position: fixed; top: 40%; left: 0; transform: translateY(-50%); width: 100px; text-align: center; } .left-nav ul { list-style: none; margin: 0; padding: 0; } .left-nav li { margin-bottom: 10px; } .left-nav li a { display: block; background-color: #f9f9f9; color: #333; padding: 10px; border-radius: 5px; text-decoration: none; } .left-nav li a:hover { background-color: #333; color: #fff; } /* 广告浮动窗口样式 */ .ad { position: fixed; top: 50%; right: 10px; transform: translateY(-50%); } .ad img { display: block; width: 200px; height: 200px; border-radius: 50%; box-shadow: 0px 0px 10px rgba(0, 0, 0, 0.5); }

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值