asp.net 外部 JavaScript 文件找不到 Control.ClientID

发布于 2024-09-07 01:30:53 字数 1282 浏览 1 评论 0原文

加载时,我会调用 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

猫瑾少女 2024-09-14 01:30:53

您可以使用面板和隐藏它的代码隐藏来显示您的标记吗?

Visible 属性设置为 false 和将 style display 属性设置为 none 之间存在差异 - 第一个根本不会渲染元素,这意味着没有任何渲染与您要查找的 id 一起。

编辑:这可能是因为您在超时中调用 HideControl 的方式 - 这应该是一个函数而不是字符串。

为了清楚起见,尝试这样做

function ToggleAlert(_c) {
    setTimeout( 
        function () { 
            HideControl(_c); 
        }, 3000);
}

,当您将字符串传递给 setTimeout 时,它会被评估然后运行。 eval 生成的代码块将在与您的 ToggleAlert 方法不同的作用域中运行,因此 _c 此时将不可用。

编辑:您还需要实际获取对该控件的引用。您将 id 字符串传递给 ToggleAlert,后者将其转发给 HideControl,后者需要一个对象而不是字符串。

function HideControl(_c) { // _c is the id of the element
    var control = document.getElementById(_c);
    if (control != null)
        control.style.display = 'none';
}

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 style display 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

function ToggleAlert(_c) {
    setTimeout( 
        function () { 
            HideControl(_c); 
        }, 3000);
}

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 your ToggleAlert 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 to HideControl, which is expecting an object not a string.

function HideControl(_c) { // _c is the id of the element
    var control = document.getElementById(_c);
    if (control != null)
        control.style.display = 'none';
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文