c# - 如何将此 javascript 函数放置在外部文件中以供用户控制?

标签 c# javascript asp.net

我有一个名为 header.aspx 的用户控件。在我的用户控件中,如果我这样做,它就会起作用。

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

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

我在名为 default.aspx 的页面中使用此用户控件。我尝试使用 src="~scripts/foo.js"。如果我这样做是行不通的。我想这个问题很简单。我如何在存储在外部位置(不在页面中。位于脚本文件夹中)的用户控件中调用java脚本函数。提前致谢。

最佳答案

据我了解,这显然是一个路径问题。 只需按照以下步骤操作可能会对您有所帮助。

  1. Create a .js file first. 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. This will give you the correct path for the js file.

当我为控件创建外部 js 文件时,我遵循上述步骤。

还要确保您以其他人建议的方式调用您的函数,否则您的函数将不会被调用:

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

关于c# - 如何将此 javascript 函数放置在外部文件中以供用户控制?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24298740/

相关文章:

javascript 对象原型(prototype)属性未更新

javascript - 用于表单验证的 JQuery 插件

c# - 将 xml 数据发布到 .net webservice asmx 时出现问题

c# - 停止 SQL Server - 使用 C#

c# - LINQ 对数据进行两次分组

c# - 是否有任何时区实际正确使用 TimeZoneInfo IsFixedDateRule?

c# - Asp.Net Web Api - 为 IEnumerable<T> 返回 404 Get When null

c# - 以存储数据的属性为键的字典

javascript - 不确定参数如何传递给参数

c# - 使用相同数据库问题的 Web 和 Windows 窗体应用程序