解析ASP.NET 2.0創(chuàng)建母版頁引來的麻煩_.Net教程
推薦:解讀ASP.NET 2.0跨網(wǎng)頁提交的三種方法當(dāng)在一個(gè)ASP.NET Web頁面中進(jìn)行操作時(shí),在默認(rèn)情況下向當(dāng)前頁進(jìn)行提交。這種行為在ASP.NET出現(xiàn)之前并沒有什么關(guān)系,但在ASP.NET中,當(dāng)我們想向另外一個(gè)Web頁提交一個(gè)Web Form時(shí)該如何做呢?在ASP.NET1.0中這個(gè)問題變得很難解決,但在ASP.NET 2.0中為開發(fā)人員
一、問題提出
由于總體排版和設(shè)計(jì)的需要,我們往往創(chuàng)建母版頁來實(shí)現(xiàn)整個(gè)網(wǎng)站的統(tǒng)一性,最近我由于統(tǒng)一性的需要,把原來整個(gè)項(xiàng)目單獨(dú)的頁面全部套用了母版頁。但是出現(xiàn)了一個(gè)錯(cuò)誤……在這里記錄一下,方便大家參考。
二、抽象模型
由于整個(gè)頁面內(nèi)容過多,所以我把這個(gè)頁面中最為本質(zhì)的問題抽象出來。
原來單一頁面,就是利用按鈕觸發(fā)JS事件,在文本域中插入“(_)”功能,其實(shí)現(xiàn)代碼如下:
以下是引用片段:
| 以下為引用的內(nèi)容: <%@ 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="服務(wù)器端插入(_)" OnClientClick="insert();"/> <input id="btnInsert2" name="insert" onclick="insert();" type="button" value="客戶端插入(_)" runat="server"/></div> </form> </body> </html> |
上述頁面可以正常使用。后來使用模板頁后,其代碼如下:
以下是引用片段:
| 以下為引用的內(nèi)容: <%@ 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="服務(wù)器端插入(_)" OnClientClick="insert();"/> <input id="btnInsert2" name="insert" onclick="insert();" type="button" value="客戶端插入(_)" runat="server"/></div> </asp:Content> |
當(dāng)打開后按下按鈕出現(xiàn)了“Microsoft JScript 運(yùn)行時(shí)錯(cuò)誤: 'document.getElementById(...)' 為空或不是對(duì)象”。這是什么原因呢?原來好好的,怎么套用個(gè)母版頁就出現(xiàn)這個(gè)奇怪的問題呢?困擾了好久,和朋友討論了一下,終于找到了答案……
三、分析本質(zhì)
原來我們仔細(xì)看看其生成的HTML代碼:
單一頁面:
以下是引用片段:
| 以下為引用的內(nèi)容: <!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="服務(wù)器端插入(_)" 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> |
分享:揭秘設(shè)計(jì)ASP.NET應(yīng)用程序的七大妙招隨著微軟.NET的流行,ASP.NET越來越為廣大開發(fā)人員所接受。作為ASP.NET的開發(fā)人員,我們不僅需要掌握其基本的原理,更要多多實(shí)踐,從實(shí)踐中獲取真正的開發(fā)本領(lǐng)。在我們的實(shí)際開發(fā)中,往往基本的原理滿足不了開發(fā)需求,我們更多的要積累一些開發(fā)技巧,本文就
- asp.net如何得到GRIDVIEW中某行某列值的方法
- .net SMTP發(fā)送Email實(shí)例(可帶附件)
- js實(shí)現(xiàn)廣告漂浮效果的小例子
- asp.net Repeater 數(shù)據(jù)綁定的具體實(shí)現(xiàn)
- Asp.Net 無刷新文件上傳并顯示進(jìn)度條的實(shí)現(xiàn)方法及思路
- Asp.net獲取客戶端IP常見代碼存在的偽造IP問題探討
- VS2010 水晶報(bào)表的使用方法
- ASP.NET中操作SQL數(shù)據(jù)庫(連接字符串的配置及獲取)
- asp.net頁面?zhèn)髦禍y(cè)試實(shí)例代碼
- DataGridView - DataGridViewCheckBoxCell的使用介紹
- asp.net中javascript的引用(直接引入和間接引入)
- 三層+存儲(chǔ)過程實(shí)現(xiàn)分頁示例代碼
.Net教程Rss訂閱編程教程搜索
.Net教程推薦
- 解析兩種ASP.Net數(shù)據(jù)庫連接設(shè)置和讀取方法
- dropdownlist,datagrid數(shù)據(jù)綁定簡(jiǎn)單實(shí)現(xiàn)
- GridView中動(dòng)態(tài)設(shè)置CommandField是否可用或可見的小例子
- VB.NET 中刪除DataGridView中所選行的小例子
- 解析VB.NET中的常量與枚舉功能
- Attribute高級(jí)應(yīng)用:簡(jiǎn)化ANF自定義控件初始化過程
- 新手入門之ASP.NET2.0中的緩存技術(shù)解析
- 解析如何使GridView每頁添加不重新開始序號(hào)列
- 解讀鏈表的順序表示和實(shí)現(xiàn)
- 關(guān)于IronPython和C#執(zhí)行速度對(duì)比
- 相關(guān)鏈接:
- 教程說明:
.Net教程-解析ASP.NET 2.0創(chuàng)建母版頁引來的麻煩
。