简体   繁体   English

来自 asp.net C# 页面的外部 javascript 文件

[英]external javascript file from asp.net C# page

i have some javascript functions written in an external javascript file.我在外部 javascript 文件中编写了一些 javascript 函数。 and i have included the file in my asp.net page head section.我已将该文件包含在我的 asp.net 页头部分中。 however im not able to understand how do i call the functions from the code behind file.但是我无法理解如何从文件后面的代码中调用函数。 eg, i want to call a certain function 'tacount' on onkeypress event of a textbox.例如,我想在文本框的 onkeypress 事件上调用某个 function 'tacount'。 how can i possible do that?我怎么可能做到这一点?

You can't call client side functions from server side code.您不能从服务器端代码调用客户端函数。

You could emit javascript to call these functions from your server side code, but this can get very messy.可以发出 javascript 从服务器端代码调用这些函数,但这会变得非常混乱。

Instead of attaching javascript functions to server side events, do so on the client side.不要将 javascript 函数附加到服务器端事件,而是在客户端执行此操作。

Use a library such as jQuery to attach the event to your textbox on the client side.使用诸如 jQuery 之类的库将事件附加到客户端的文本框。

Have a look here at all the javascript calls you can make from code behind but basically all you have to do in your code behind call the following: 在这里查看您可以从代码后面进行的所有 javascript 调用,但基本上您必须在代码后面调用以下代码:

ScriptManager.RegisterStartupScript(this,this.getType(),"tacount",'tacount();',true);

This will then be triggered once the post back is completed on the client side, it is however advised to rather call javascript only from client side.这将在客户端完成回发后触发,但建议仅从客户端调用 javascript。

EDIT :编辑

The fact that your file is in an external javascript file does not matter given that the browser will call it the same as inline javascript.您的文件位于外部 javascript 文件中的事实并不重要,因为浏览器将调用它与内联 javascript 相同。

All you have to do is call the function as usual in your designer code ie您所要做的就是像往常一样在您的设计器代码中调用 function,即

<asp:TextBox runat="server" onkeypress='return tacount();'/>

If you want to call a JavaScript function from your Code Behind file, you should use the code as suggested by @TBohnen.jnr ie :如果您想从您的代码隐藏文件中调用 JavaScript function,您应该使用@TBohnen.jnr 建议的代码,即:

ScriptManager.RegisterStartupScript(this,this.getType(),"fnMyFunctionNameFromServiceSide_tacount",'tacount();',true);

However, you should note that the fnMyFunctionNameFromServiceSide_tacount should not be present in your markup code or any included javascript file.但是,您应该注意fnMyFunctionNameFromServiceSide_tacount不应出现在您的标记代码或任何包含的 javascript 文件中。

If you want to call the javascript function from an externally included javascript file use the following code:如果要从外部包含的 javascript 文件中调用 javascript function,请使用以下代码:

<asp:TextBox id="myTextBox" runat="server" onkeypress="javascript: return tacount();"/>

However, if the above does not work for you then it means that the function could not be found.但是,如果上述方法对您不起作用,则意味着找不到 function。 In this case I would suggest you to use FireBug to find the exact cause for not finding the javascript function.在这种情况下,我建议您使用FireBug查找未找到 javascript function 的确切原因。 Could be that the function itself is not present or there is some javascript error inside the function itself.可能是 function 本身不存在,或者 function 本身内部存在一些 javascript 错误。

Hope this helps.希望这可以帮助。

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

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