简体   繁体   English

如何将此Java脚本函数放在外部文件中以进行用户控制?

[英]How to place this java script function in an external file for user control?

I have a user control called header.aspx. 我有一个名为header.aspx的用户控件。 In my user control if I do this it will work. 在我的用户控件中,如果我这样做,它将起作用。

<script type="text/javascript">
    function greeting(){
        Alert("hi");
    }
</script>

 <asp:button id="button1" OnClientClick="greeting" /> </asp:button>

I am using this user control in a page called default.aspx. 我在名为default.aspx的页面中使用此用户控件。 I tried using src="~scripts/foo.js". 我尝试使用src =“〜scripts / foo.js”。 If I do that it does not work. 如果我这样做,那是行不通的。 The question is pretty simple I guess. 我想这个问题很简单。 How would I call a java script function in a user control which is stored in an external location( not in the page. Located in the scripts folder). 我将如何在用户控件中调用Java脚本函数,该函数存储在外部位置(不在页面中。位于scripts文件夹中)。 Thanks in advance. 提前致谢。

As I can understand this is clearly a path issue. 据我了解,这显然是一个路径问题。 Just follow these steps might help you. 只需按照以下步骤可能会为您提供帮助。

  1. Create a .js file first. 首先创建一个.js文件。 Put your code and save it in the folder you want it to. 将您的代码并将其保存在所需的文件夹中。
  2. Now Drag and Drop the js file inside the head section of your html code from the Solution Explorer window. 现在,从“解决方案资源管理器”窗口中将js文件拖放到html代码的头部内。 This will give you the correct path for the js file. 这将为您提供js文件的正确路径。

The above steps is what I follow, when I create an external js file for my controls. 当我为控件创建外部js文件时,将执行上述步骤。

Also make sure you call your function in this manner also suggested by others Else your function won't get call: 还请确保您以其他人建议的方式调用函数,否则您的函数将不会被调用:

<asp:button id="button1" OnClientClick="greeting();" /> </asp:button>

只需使用以下代码:

 <script src="<%: ResolveUrl("~/Scripts/foo.js") %>"></script> 

Script: test.js 脚本:test.js

    function greeting() {
    alert("hi");
    return false;
}

user control: <asp:Button ID="button1" OnClientClick="return greeting()" runat="server" Text="click" /> 用户控件: <asp:Button ID="button1" OnClientClick="return greeting()" runat="server" Text="click" />

Page: 页:

    <head runat="server">
    <title></title>
    <script src="test.js"></script>
</head>
<body>
    <form id="form1" runat="server">
    <div>
    <asp:temp ID="temp" runat="server" />
    </div>
    </form>
</body>
</html>

This should work now. 现在应该可以使用了。

 <asp:button id="button1" OnClick=" javascript : greeting();" /> </asp:button>

try to use it. 尝试使用它。 havent trie but i think it should work. 避难所,但我认为这应该工作。

First of all you need to create a seprate javascript file and then add this in your page in this way. 首先,您需要创建一个单独的javascript文件,然后以这种方式将其添加到页面中。 add this tag in the tag of your page. 将此标签添加到页面的标签中。

use 采用

OnClientClick="greeting()"

you missed "()" in OnClientClick="greeting" 您在OnClientClick =“ greeting”中错过了“()”

Please see the whole html code: 请查看完整的html代码:

<!DOCTYPE html>

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
    <script src="scripts/foo.js"></script>
</head>
<body>
    <form id="form1" runat="server">
    <div>
    <asp:button id="button1" runat="server" OnClientClick="greeting()" Text="hit" />
    </div>
    </form>
</body>
</html>

Thanks. 谢谢。

External javascript file reference: 外部javascript文件参考:

<script src="yourpath/yourfilename.js"></script>

Button Control 按钮控制

<asp:Button ID="button1" OnClientClick="greeting();" runat="server" Text="click" />

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM