ASP.NET 2.0建立主版頁面引來的麻煩

來源:互聯網
上載者:User

本文將為大家介紹一下 ASP.NET 2.0 在建立主版頁面時引來的麻煩,並分析了 問題產生的實質,大家在實際操作中多多注意一下。

一、問題提出

由於總體排版和設計的需要,我們往往建立主版頁面來實現整個網站的統一性 ,最近我由於統一性的需要,把原來整個項目單獨的頁面全部套用了主版頁面。但 是出現了一個錯誤……在我的Blog中記錄一下,方便大家參考。

二、抽象模型

由於整個頁面內容過多,所以我把這個頁面中最為本質的問題抽象出來。

原來單一頁面,就是利用按鈕觸發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(...)' 為空白或不是對象”。這是什麼原因呢?原來好 好的,怎麼套用個主版頁面就出現這個奇怪的問題呢?困擾了好久,和朋友討論了 一下,終於找到了答案……

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.