您所在的位置: 首页 > 开发 > .Net > ASP.NET >

ASP.NET 2.0创建母版页引来的麻烦(1)

http://developer.51cto.com  2007-11-01 12:47  YJingLee  博客园  我要评论(0)
  • 摘要:本文将为大家介绍一下ASP.NET 2.0在创建母版页时引来的麻烦,并分析了问题产生的实质,大家在实际操作中多多注意一下。
  • 标签:ASP.NET 2.0  创建  母版页  ASP.NET

【引自YJingLee的博客】一、问题提出

由于总体排版和设计的需要,我们往往创建母版页来实现整个网站的统一性,最近我由于统一性的需要,把原来整个项目单独的页面全部套用了母版页。但是出现了一个错误……在这里记录一下,方便大家参考。

二、抽象模型

由于整个页面内容过多,所以我把这个页面中最为本质的问题抽象出来。

原来单一页面,就是利用按钮触发JS事件,在文本域中插入“(_)”功能,其实现代码如下:

以下是引用片段:
<%@ 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">
<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
<title>单一页面抽象模型-YJingLee</title>
<script language="javascript" type="text/javascript">
// <!CDATA[
function insert() {
document.getElementById("txt").value=document.getElementById("txt").value+"(__)";

return;
}
// ]]>
</script>
</head>
<body>
<form id="form1" runat="server">
<div>
<textarea id="txt" runat="server" name="txt"

rows="10" cols="50"></textarea>
<asp:Button ID="btnInsert" runat="server"

Text="服务器端插入(_)"  OnClientClick="insert();"/>
<input id="btnInsert2" name="insert" onclick="insert();"

type="button" value="客户端插入(_)" runat="server"/></div>
</form>
</body>
</html>

上述页面可以正常使用。后来使用模板页后,其代码如下:

以下是引用片段:
<%@ Page Language="C#" MasterPageFile="~/MasterPage.master"
   AutoEventWireup="true" CodeFile="Default2.aspx.cs" 
   Inherits="Default2" Title="使用母版页面抽象模型-YJingLee" %> 
<asp:Content ID="Content1" ContentPlaceHolderID="ContentPlaceHolder1" Runat="Server">
<script language="javascript" type="text/javascript">
// <!CDATA[
function insert() {
document.getElementById("txt").value=document.getElementById("txt").value+"(__)";
        return; 
}
// ]]>
</script>
<div>
<textarea id="txt" runat="server" name="txt"
           rows="10" cols="50"></textarea> 
<asp:Button ID="btnInsert" runat="server" Text="服务器端插入(_)" 
           OnClientClick="insert();"/> 
<input id="btnInsert2" name="insert" onclick="insert();"
           type="button" value="客户端插入(_)" runat="server"/></div> 
</asp:Content>

当打开后按下按钮出现了“Microsoft JScript 运行时错误: 'document.getElementById(...)' 为空或不是对象”。这是什么原因呢?原来好好的,怎么套用个母版页就出现这个奇怪的问题呢?困扰了好久,和朋友讨论了一下,终于找到了答案……

三、分析本质

原来我们仔细看看其生成的HTML代码:

单一页面:

以下是引用片段:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
   "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> 
<html xmlns="http://www.w3.org/1999/xhtml" >
<head><title>    单一页面抽象模型-YJingLee</title>
<script language="javascript" type="text/javascript">
// <!CDATA[
function insert() {
document.getElementById("txt").value=document.getElementById("txt").value+"(__)";
return;
}
// ]]>
</script>
</head>
<body>
<form name="form1" method="post" action="Default.aspx" id="form1">
<div>
<input type="hidden" name="__VIEWSTATE" id="__VIEWSTATE"
   value="/wEPDwUKMTEzMjE5NDA0NWRkKlEH1jSXJkIbnUaP2d9Dra8LQEk=" /> 
</div>
<div>
<textarea name="txt" id="txt" rows="10" cols="50"></textarea>
<input type="submit" name="btnInsert" value="服务器端插入(_)"
   onclick="insert();" id="btnInsert" /> 
<input name="btnInsert2" type="button" id="btnInsert2"
   onclick="insert();" value="客户端插入(_)" /></div> 
<div>
<input type="hidden" name="__EVENTVALIDATION" id="__EVENTVALIDATION"
   value="/wEWBALVid/5DQKShrDCCQL5w9POBQL5w4vOBZPGqxUU/yvoKTqG8k+uG8YroGTv" /> 
</div></form>
</body>
</html>


共2页: 1 [2] 下一页
【内容导航】
 第 1 页:问题提出  第 2 页:总结
ASP.NET视频教程
ASP.NET MVC框架视频教程
专题:ASP.NET 2.0基础开发指南
微软的Ajax:Atlas(ASP.net AJAX)
ASP.NET开发教程
 
 验证码: (点击刷新验证码)   匿名发表
  • 亮剑.NET. 图解C#开发实战

  • 作者:李新峰 付志涛 缪勇
  • 本书采用全新的图解思路,分3篇介绍使用微软C#语言开发实际应用程序的基本知识。第1篇包括10章,介绍了C#语言的基础知识,主要..
Copyright©2005-2009 51CTO.COM 版权所有