style="MARGIN-TOP: 0px; FLOAT: left" border="0" marginwidth="0" framespacing="0" marginheight="0" src="http://wz.csdn.net/vote.aspx?t=%u91C7%u7528%u6BCD%u677F%u9875%u7684%u9875%u9762%u4F7F%u7528JavaScript%u95EE%u9898%20-%20%u5C0F%u7C73%u7684%u4E13%u680F%20-%20CSDNBlog&u=http%3A//blog.csdn.net/a311300/archive/2006/12/12/1439840.aspx" frameborder="0" noresize="noresize" width="54" scrolling="no" height="75">
采用母板页的页面使用JavaScript问题 <script src="http://blog.csdn.net/count.aspx?ID=1439840&Type=Rank" type="text/javascript"></script>
采用母板页的页面,在使用JavaScript时,会发现.net中的控件id,无法获取.
在调试状态下,生成静态页下查看,原来编译器将masterpage当成一个控件,而其中的控件也都会相应的改变id.
会变id为ctl00$模板名称$原名称.例:ctl00$ContentPlaceHolder1$txtSouce
下面写一个例子:
<
script language
=
"
javascript
"
type
=
"
text/javascript
"
>
function CheckInfo()
... {
//如果点击"重置"按钮
if(event.srcElement.id=='btnClear')
...{
document.getElementById("ctl00$ContentPlaceHolder1$txtTitle").innerText = "";
document.getElementById("ctl00$ContentPlaceHolder1$txtScore").innerText = "";
document.getElementById("ctl00$ContentPlaceHolder1$txtStartDate").innerText = "";
document.getElementById("ctl00$ContentPlaceHolder1$txtEndDate").innerText = "";
document.getElementById("ctl00$ContentPlaceHolder1$txtContent").innerText = "";
document.getElementById("ctl00$ContentPlaceHolder1$txtSouce").innerText = "";
}
}
</ script >
< td colspan = 3 width = " 75% " >
< asp:TextBox ID = " txtTitle " runat = " server " Width = " 90% " ></ asp:TextBox >
td >
function CheckInfo()
... {
//如果点击"重置"按钮
if(event.srcElement.id=='btnClear')
...{
document.getElementById("ctl00$ContentPlaceHolder1$txtTitle").innerText = "";
document.getElementById("ctl00$ContentPlaceHolder1$txtScore").innerText = "";
document.getElementById("ctl00$ContentPlaceHolder1$txtStartDate").innerText = "";
document.getElementById("ctl00$ContentPlaceHolder1$txtEndDate").innerText = "";
document.getElementById("ctl00$ContentPlaceHolder1$txtContent").innerText = "";
document.getElementById("ctl00$ContentPlaceHolder1$txtSouce").innerText = "";
}
}
</ script >
< td colspan = 3 width = " 75% " >
< asp:TextBox ID = " txtTitle " runat = " server " Width = " 90% " ></ asp:TextBox >
td >
调用:
<
input ID
=
"
btnClear
"
class
=
"
buttomw
"
value
=
"
重置
"
onclick
=
"
CheckInfo();
"
type
=
"
button
"
/>