asp.net 外部 JavaScript 文件找不到 Control.ClientID
加载时,我会调用 JavaScript setTimeout() 函数来隐藏 .NET Panel 控件,并在第一次加载时将其隐藏在后面的代码中。单击“保存”按钮会将面板设置为可见,然后重新加载页面,此时将调用 setTimeout() 函数...所以基本上您单击“保存”,并看到一个带有“详细信息已保存”的面板三秒钟,此时它消失。
问题是外部 JavaScript 文件找不到 _pDivAlert.ClientID (我已经调试过,它返回 null)。仅当代码位于 .aspx 页面的标记中时它才有效。关于如何将客户端 ID 传递给 HideControl() 函数或从外部 JS 文件查找 ClientID,有什么建议吗?
这是我的代码,有什么建议吗?
<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>
我还尝试在 ToggleAlert() 调用中发送 ClientID,但这不起作用:
<body onload="ToggleAlert('<%=_pDivAlert.ClientID %>')">
External JS:
function HideControl(_c) {
var control = _c;
if (control != null)
control.style.display = 'none';
}
function ToggleAlert(_c) {
setTimeout("HideControl(_c)", 3000);
}
On load I'm both calling a JavaScript setTimeout() function that will hide a .NET Panel control, and hiding it in the code behind on first load. Clicking the save button will set the Panel to visible then reload the page at which point a setTimeout() function is called... so basically you click save, and see a panel with "Details Saved" for three seconds, at which point it disappears.
The problem is the external JavaScript file can't find _pDivAlert.ClientID (I've debugged and it returns null). It only works when the code is in a tag in the .aspx page. Any suggestions as to how I can either pass the client ID to the HideControl() function or find the ClientID from the external JS file?
Here's my code, any suggestions?
<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>
I've also tried sending the ClientID within the ToggleAlert() call, but that didn't work:
<body onload="ToggleAlert('<%=_pDivAlert.ClientID %>')">
External JS:
function HideControl(_c) {
var control = _c;
if (control != null)
control.style.display = 'none';
}
function ToggleAlert(_c) {
setTimeout("HideControl(_c)", 3000);
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您可以使用面板和隐藏它的代码隐藏来显示您的标记吗?
将
Visible
属性设置为 false 和将 styledisplay
属性设置为 none 之间存在差异 - 第一个根本不会渲染元素,这意味着没有任何渲染与您要查找的 id 一起。编辑:这可能是因为您在超时中调用
HideControl
的方式 - 这应该是一个函数而不是字符串。为了清楚起见,尝试这样做
,当您将字符串传递给
setTimeout
时,它会被评估然后运行。 eval 生成的代码块将在与您的ToggleAlert
方法不同的作用域中运行,因此_c
此时将不可用。编辑:您还需要实际获取对该控件的引用。您将 id 字符串传递给
ToggleAlert
,后者将其转发给HideControl
,后者需要一个对象而不是字符串。can you show your markup with the panel and the codebehind where you hide it?
there's a difference between setting the
Visible
property to false and setting the styledisplay
attribute to none- the first will not render the element at all, meaning there isn't anything rendered with the id you're looking for.edit: it's probably because of the way you're calling
HideControl
in the timeout- this should be a function instead of a string.try doing
just for clarity, when you pass a string to
setTimeout
, it's evaluated and then run. the code chunk that eval produces will run in a different scope than yourToggleAlert
method, and so_c
won't be available at that time.edit: you also need to actually get a reference to the control. you're passing the id string to
ToggleAlert
, which relays it toHideControl
, which is expecting an object not a string.