asp.net外部JavaScript文件没有';找不到Control.ClientID
在加载时,我调用一个JavaScript setTimeout()函数来隐藏.NET面板控件,并在第一次加载时将其隐藏在后面的代码中。单击“保存”按钮将面板设置为可见,然后重新加载页面,此时调用setTimeout()函数。。。所以基本上你点击save,看到一个面板,上面有“Details Saved”三秒钟,然后它消失了 问题是外部JavaScript文件找不到pDivAlert.ClientID(我已经调试过了,它返回null)。它仅在代码位于.aspx页面的标记中时才起作用。关于如何将客户端ID传递给HideControl()函数或从外部JS文件中查找ClientID,有什么建议吗 这是我的代码,有什么建议吗asp.net外部JavaScript文件没有';找不到Control.ClientID,asp.net,javascript,clientid,Asp.net,Javascript,Clientid,在加载时,我调用一个JavaScript setTimeout()函数来隐藏.NET面板控件,并在第一次加载时将其隐藏在后面的代码中。单击“保存”按钮将面板设置为可见,然后重新加载页面,此时调用setTimeout()函数。。。所以基本上你点击save,看到一个面板,上面有“Details Saved”三秒钟,然后它消失了 问题是外部JavaScript文件找不到pDivAlert.ClientID(我已经调试过了,它返回null)。它仅在代码位于.aspx页面的标记中时才起作用。关于如何将客户
<script language="javascript" src="Forms.js" type="text/javascript"></script>
<body onload="ToggleAlert()">
<form id="form1" runat="server">
<script type="text/javascript">
//alert the user that the details were saved
function HideControl() {
var control = document.getElementById('<%=_pDivAlert.ClientID %>');
if(control != null)
control.style.display = 'none';
}
function ToggleAlert() {
setTimeout("HideControl()", 3000);
}
</script>
你能用面板和代码显示你的标记吗 将
Visible
属性设置为false与将styledisplay
属性设置为none之间存在差异-第一个属性根本不会呈现元素,这意味着没有任何内容是使用您要查找的id呈现的
编辑:这可能是因为您在超时时调用HideControl
的方式-这应该是一个函数而不是字符串
function HideControl(_c) { // _c is the id of the element
var control = document.getElementById(_c);
if (control != null)
control.style.display = 'none';
}
试着做
function ToggleAlert(_c) {
setTimeout(
function () {
HideControl(_c);
}, 3000);
}
为了清楚起见,当您将字符串传递给setTimeout
时,将对其求值,然后运行该字符串。eval生成的代码块将在与您的ToggleAlert
方法不同的范围内运行,因此\u c
届时将不可用
编辑:您还需要实际获取控件的引用。您正在将id字符串传递给TogleAlert
,后者将其转发给HideControl
,后者需要的是对象而不是字符串
function HideControl(_c) { // _c is the id of the element
var control = document.getElementById(_c);
if (control != null)
control.style.display = 'none';
}
谢谢你的回复。我使HideControl()成为一个函数,现在我在外部JS中看到了控件ID。但现在调试时,我看到“control.style.display='none'”中的“style”是“undefined”
function HideControl(_c) { // _c is the id of the element
var control = document.getElementById(_c);
if (control != null)
control.style.display = 'none';
}