母版页中对控件ID的处理。
由于总体排版和设计的需要,我们往往创建母版页来实现整个网站的统一性,最近我由于统一性的需要,把原来整个项目单独的页面全部套用了母版页。但是出现了一个错误……在这中记录一下,方便大家参考。
由于整个页面内容过多,所以我把这个页面中最为本质的问题抽象出来。原来单一页面,就是利用按钮触发JS事件,在文本域中插入“(_)”功能,其实现代码如下:
<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="服务器端插入(_)" 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="服务器端插入(_)" OnClientClick="insert();"/> <input id="btnInsert2" name="insert" onclick="insert();" type="button" value="客户端插入(_)" runat="server"/></div> </asp:content>
当打开后按下按钮出现了“Microsoft JScript 运行时错误: 'document.getElementById(...)' 为空或不是对象”。这是什么原因呢?原来好好的,怎么套用个母版页就出现这个奇怪的问题呢?困扰了好
版权所有 2021-2024 石家庄建站平台 冀ICP备2022000585号-2 24小时服务热线:15383239821