用户控制从内容页面的母版页访问 Javascript

发布于 2024-07-15 01:04:28 字数 517 浏览 5 评论 0原文

大家好,我有一个问题。 我有一个母版页,我的所有内容页面都继承自该母版页。 在此母版页中,我有一个脚本标记指向 Javascript 文件夹 ~/Scripts/validation.js

在我的内容页面上,我使用不同的用户控件,这些用户控件需要使用 中的许多功能validation.js 文件,但是如果我不将

当然,我可以将 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 技术交流群。

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

发布评论

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

评论(4

能怎样 2024-07-22 01:04:28

你想要做的事情应该有效,所以我怀疑你的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.

总攻大人 2024-07-22 01:04:28

我从 ScottGu 这里

将其添加到您的用户控件中,这会在用户控件中启用智能感知,但始终评估为 false:

<% if (false) { %>
   <script src="/Scripts/validation.js" type="text/javascript"></script>
<% } %>

I picked up this tip from ScottGu here.

Add this to you user control which enables Intellisense in user controls but always evaluates false:

<% if (false) { %>
   <script src="/Scripts/validation.js" type="text/javascript"></script>
<% } %>
時窥 2024-07-22 01:04:28

我目前正在我的网站中执行此操作,方法是转到母版页上的源代码并将以下内容放入 ContentPlaceHolder 的头部和外部。

<head>
    <asp:ContentPlaceHolder ID="HeadContent" runat="server">
    </asp:ContentPlaceHolder>

    <script src="CP.js" type="text/javascript"></script>
</head>

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.

<head>
    <asp:ContentPlaceHolder ID="HeadContent" runat="server">
    </asp:ContentPlaceHolder>

    <script src="CP.js" type="text/javascript"></script>
</head>
满天都是小星星 2024-07-22 01:04:28

您为 js 文件分配的路径可能在所有页面中都不匹配。

script src="../JavaScript/Scriptaculous/scriptaculous.js"

如果您有单独的脚本、母版页、页面和页面文件夹,则它应该具有与上面类似的内容。 控制。

The path you are assigning for your js file is probably not matching in all the pages.

script src="../JavaScript/Scriptaculous/scriptaculous.js"

It should have something like above this if you have separate folder for Scripts, MasterPages, Pages & Controls.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文