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

推荐订阅源

Engineering at Meta
Engineering at Meta
博客园_首页
H
Help Net Security
WordPress大学
WordPress大学
让小产品的独立变现更简单 - ezindie.com
让小产品的独立变现更简单 - ezindie.com
罗磊的独立博客
博客园 - 三生石上(FineUI控件)
B
Blog
I
InfoQ
SecWiki News
SecWiki News
T
Tailwind CSS Blog
Spread Privacy
Spread Privacy
freeCodeCamp Programming Tutorials: Python, JavaScript, Git & More
V
Vulnerabilities – Threatpost
N
Netflix TechBlog - Medium
P
Palo Alto Networks Blog
CTFtime.org: upcoming CTF events
CTFtime.org: upcoming CTF events
Vercel News
Vercel News
Threat Intelligence Blog | Flashpoint
Threat Intelligence Blog | Flashpoint
K
Kaspersky official blog
M
MIT News - Artificial intelligence
S
Schneier on Security
T
Threat Research - Cisco Blogs
F
Fortinet All Blogs
Cyberwarzone
Cyberwarzone
Scott Helme
Scott Helme
aimingoo的专栏
aimingoo的专栏
Martin Fowler
Martin Fowler
MyScale Blog
MyScale Blog
The Cloudflare Blog
Recent Announcements
Recent Announcements
Security Latest
Security Latest
G
GRAHAM CLULEY
IT之家
IT之家
Y
Y Combinator Blog
The Last Watchdog
The Last Watchdog
腾讯CDC
Google DeepMind News
Google DeepMind News
V
V2EX
S
Securelist
TaoSecurity Blog
TaoSecurity Blog
B
Blog RSS Feed
S
SegmentFault 最新的问题
博客园 - 叶小钗
P
Proofpoint News Feed
云风的 BLOG
云风的 BLOG
Project Zero
Project Zero
G
Google Developers Blog
Google DeepMind News
Google DeepMind News
F
Full Disclosure

博客园 - 啊不才

【转载备用】Linux内核编译 幸运的Windows 7 Party 社区活动 jQueryinAction学习笔记——01 - 啊不才 - 博客园 django的字符替换问题 如何在屏幕中央打开一个特定的窗口 KB937061和KB947738多次安装问题 BlogEngine.Net的皮肤 [How Do I]系列学习笔记——001:学习一些技巧 继承类中override和new的区别 我提交的ACM题库的答案 『框架设计(第2版)CLR Via C#』学习笔记——使用is和as操作符来进行强制类型转换 调用Master页面上的属性 NotePad++很好用,但是我真的不想再用它了 BlogEngine的SQL Server数据库配置 NHiBernate学习笔记(1) 使用JMail.NET时遇到的问题 ToString()方法与Convert.ToString()的差异 『框架设计(第2版)CLR Via C#』学习笔记——常量 【已解决,看后文】使用BlogEngine.net的扩展插件Silverlight Player Extension遇到的问题
关于asp:ScriptManager与Script代码块的位置关系问题
啊不才 · 2008-05-09 · via 博客园 - 啊不才

在学校Ajax时遇到了一个小问题,先来看一下原来的代码:

 1<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="WebForm1.aspx.cs" Inherits="MSDNWebCast_20070621.WebForm1" %>
 2
 3<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
 4
 5<html xmlns="http://www.w3.org/1999/xhtml" >
 6<head runat="server">
 7    <title>Untitled Page</title>
 8    <script language="javascript" type="text/javascript">
 9    Type.registerNamespace('Demo');
10    
11    Demo.Person = function(firstName, lastName, emailAddress)
12    {
13        this._firstName = firstName;
14        this._lastName = lastName;
15        this._emailAddress = emailAddress;
16    }

17
</script>
18</head>
19<body>
20    <form id="form1" runat="server">
21    <asp:ScriptManager ID="ScriptManager1" runat="server">
22    </asp:ScriptManager>
23    <div>
24    
25    </div>
26    </form>
27</body>
28
29</html>
30

在运行时会弹出如下图所示的错误:

然后我就纳闷了,怎么会这样?很是郁闷~~!其实我们只要把<Script>代码块放到asp:ScriptManager的后面,问题就解决了。
正确的代码如下:

 1<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="WebForm1.aspx.cs" Inherits="MSDNWebCast_20070621.WebForm1" %>
 2
 3<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
 4
 5<html xmlns="http://www.w3.org/1999/xhtml" >
 6<head runat="server">
 7    <title>Untitled Page</title>
 8</head>
 9<body>
10    <form id="form1" runat="server">
11    <asp:ScriptManager ID="ScriptManager1" runat="server">
12    </asp:ScriptManager>
13    <div>
14    
15    </div>
16    </form>
17</body>
18<script language="javascript" type="text/javascript">
19    Type.registerNamespace('Demo');
20    
21    Demo.Person = function(firstName, lastName, emailAddress)
22    {
23        this._firstName = firstName;
24        this._lastName = lastName;
25        this._emailAddress = emailAddress;
26    }

27
</script>
28</html>
29

个人分析原因可能是浏览器在解析文档时是顺序解析的,所以在asp:ScriptManager之前代码是不认识Type为何物的。自然就会报错,说Type未定义。因此我们在以后添加Javascript代码时要注意放到asp:ScriptManager的后面。