为什么我的用户控件 pageLoad 函数在某些 ASP.net 页面上未触发?
我正在开发 ASP.net 应用程序。
我设计的名为 LocationSelector
的用户控件存在问题。它工作得很好,但我无法在 ASP:UpdatePanel 中使用它。通过浏览 SO,我了解到,我需要将内联 javascript 放入外部文件中,并将其注册到 ScriptManager 中,这样就不会出现 Response.Write 调用JavaScript。
接下来,我发现该控件最初可以工作,但是当我将其包含在 ASP:UpdatePanel 中时,它会在第一次 AJAX 回发后停止工作。我读到要解决这个问题,我需要用 function pageLoad()
替换我的 $(document).ready
调用,该调用会在每个 ASP.net 页面加载时自动执行。
我的 ASP.net 应用程序中的几个页面运行良好(框架驱动应用程序的效果给我留下了深刻的印象)。然而,令我沮丧的是,我发现我的 LocationSelector
控件在我的三个页面上根本不再工作。 HTML 呈现,但 pageLoad(使用 jQuery 在控制元素上绑定事件)不会触发。
我验证了这一点,但将 alert('pageLoad');
作为我的 pageLoad
函数的第一行。在有效的页面上,我看到了警报。在不起作用的页面上,即使在初始页面加载时,我也从未看到警报。
什么样的问题可能导致我的用户控件 LocationSelector
上的 pageLoad
函数在某些页面上执行,但在其他页面上不执行?
I'm working on an ASP.net application.
I had a problem with a user control I'd designed called LocationSelector
. It worked great, but I couldn't use it within an ASP:UpdatePanel. I learned, by browsing SO, that I needed to put my in-line javascript in an external file and register it with ScriptManager
so that there are no Response.Write
calls in the Javascript.
Next, I discovered that the control worked at first, but when I enclosed it within the ASP:UpdatePanel, it would stop functioning after the first AJAX postback. I read that to fix that, I needed to replace my $(document).ready
call with function pageLoad()
which is automatically executed with each ASP.net page load.
Several of my pages in my ASP.net application work fine (and I was really impressed with how well the framework seemed to drive the application). However, to my dismay, I discovered that my LocationSelector
control no longer works at all on three of my pages. The HTML renders, but the pageLoad (which uses jQuery to bind events on control elements) does not fire.
I verified this but putting alert('pageLoad');
as the first line of my pageLoad
function. On the pages that work, I see the alert. On the pages that don't work, I never see the alert, even on the initial page load.
What kind of problem could be causing the pageLoad
function on my user control LocationSelector
to execute on some pages, but not others?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我已经找到了问题,但我仍然不知道解决方案是什么。
我的用户控件
Location Selector
位于相对目录~/Controls
中。我使用以下代码注册脚本:问题在于 JavaScript 文件 Controls/LocationSelector.js 的相对路径。
使用此代码的页面都位于 ASP.net 应用程序的根目录中。不起作用的页面位于子目录中。
I've found the problem, but I still don't know what the solution is.
My user control
Location Selector
is in the relative directory~/Controls
. I register the scripts using the following code:The problem is with the relative path to the JavaScript file Controls/LocationSelector.js.
The pages that work with this code are all in the root directory of the ASP.net application. The pages that don't work are in subdirectories.