用户控制从内容页面的母版页访问 Javascript
大家好,我有一个问题。 我有一个母版页,我的所有内容页面都继承自该母版页。 在此母版页中,我有一个脚本标记指向 Javascript 文件夹 ~/Scripts/validation.js
在我的内容页面上,我使用不同的用户控件,这些用户控件需要使用 中的许多功能validation.js
文件,但是如果我不将 标记和 Javascript 函数放在内容页上的 contentholder 中,用户控件将看不到这些函数,并且会收到类似
OnNameValidation
未定义。
当然,我可以将 Javascript 代码复制到所有页面中,但那是 30 多个页面,如果我在其中一个 Javascript 函数中发现错误,这将是维护的噩梦。
所以问题(如果您还没有从我的长篇论文中弄清楚)是如何使用validation.js文件的路径声明脚本标记,以便内容页及其用户控件等可以访问函数/代码。
Hello all I have a problem. I have a masterpage that all of my Content pages inherit from. Within this masterpage I have a script tag pointing to the Javascript file folder ~/Scripts/validation.js
On my content pages I use different usercontrols that require the use of many of the functions within the validation.js
file however if I dont put the <script>
tag and the Javascript functions within a contentholder on the contentpage the usercontrols do not see these functions and I get errors like OnNameValidation
is not defined.
Of course I can copy the Javascript code into all of the pages but that's 30+ pages and a maintenance nightmare if I find a bug in one of the Javascript functions.
So the question (if you haven't already figured out from my long dissertation) is how can I declare the script tag with the path to the validation.js file so that contentpages and their usercontrols etc. can access the functions/code.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
你想要做的事情应该有效,所以我怀疑你的javascript文件的路径是错误的(没有看到你的html代码我只能假设)。 请记住,如果 HEAD runat="server" 标记中有链接,则只能像这样引用 javascript 文件:“~/Scripts/validation.js”。 如果没有 runat="server" 它将找不到该文件。 你必须做类似“../scripts/validation.js”的事情
作为测试,我会尝试在母版页中调用你的javascript函数,这样你就可以排除错误的文件引用。
What you are trying to do should work, so I suspect that the path to your javascript file is wrong (without seeing your html code I can only assume). Keep in mind that you can only reference the javascript file like this: "~/Scripts/validation.js" if you have the link in a HEAD runat="server" tag. Without the runat="server" it won't find the file. You would have to do something like "../scripts/validation.js"
As a test I would try to call your javascript function in the masterpage, so you can rule out a bad file reference.
我从 ScottGu 这里。
将其添加到您的用户控件中,这会在用户控件中启用智能感知,但始终评估为 false:
I picked up this tip from ScottGu here.
Add this to you user control which enables Intellisense in user controls but always evaluates false:
我目前正在我的网站中执行此操作,方法是转到母版页上的源代码并将以下内容放入 ContentPlaceHolder 的头部和外部。
I am currently doing this in my site by going to the Source code on the master page and putting the following in the head and outside of the ContentPlaceHolder.
您为 js 文件分配的路径可能在所有页面中都不匹配。
如果您有单独的脚本、母版页、页面和页面文件夹,则它应该具有与上面类似的内容。 控制。
The path you are assigning for your js file is probably not matching in all the pages.
It should have something like above this if you have separate folder for Scripts, MasterPages, Pages & Controls.