服務(wù)熱線
153 8323 9821
母版頁中對控件ID的處理。
由于總體排版和設(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)代碼如下:
<head id="Head1" 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>
上述頁面可以正常使用。后來使用模板頁后,其代碼如下:
<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(...)' 為空或不是對象”。這是什么原因呢?原來好好的,怎么套用個(gè)母版頁就出現(xiàn)這個(gè)奇怪的問題呢?困擾了好