简体   繁体   English

来自ASP.Net用户控件的JS包含文件

[英]JS Include File From ASP.Net User Control

This maybe a very basic question but for the life of me I cannot figure it out. 这也许是一个非常基本的问题,但是对于我的一生,我无法弄清楚。 So I have a user control that gets loaded on a page. 所以我有一个加载到页面上的用户控件。 Well, in this user control, I'm wanting to include a js file for some functions that are only specific to that user control. 好吧,在此用户控件中,我想为仅针对该用户控件的某些功能包括一个js文件。 Inside this js file let's say I have a function called 'MyFunction'. 在这个js文件中,假设我有一个名为“ MyFunction”的函数。 In the onclick event of a div I'm wanting to call that function and I am unable to. 在div的onclick事件中,我想调用该函数,但无法执行。 Any help would be greatly appreciated. 任何帮助将不胜感激。

I'm using the ClientScriptManager of the page object and I'm supposedly successfully including the file on the page using the following call: 我正在使用页面对象的ClientScriptManager ,并且可以通过以下调用成功在页面上包含文件:

page.ClientScript.RegisterClientScriptInclude(scriptTitle,
    page.ResolveUrl(scriptUrl))

Now, after this, I'm wondering, is there anything special I need to do to be able to call a function that's in the include file? 现在,在这之后,我想知道,要调用包含文件中的函数,我需要做些特别的事情吗? like in my div: 就像在我的div中一样:

<div class="someClass" onclick="MyFunction();">Click Me</div>

i use the scriptmanager/scriptmanagerproxy 我使用scriptmanager / scriptmanagerproxy

<asp:ScriptManager runat="server">
  <Scripts>
     <asp:ScriptReference Path="~/js/myscript.js" />
  </Scripts>
</asp:ScriptManager>

So at the top of your page (in the head tag of your HTML), output the script tag that references your javascript file with your functions. 因此,在页面顶部(在HTML的head标签中),输出使用功能引用了javascript文件的script标签。 Al W's method above is neat. Al W的上述方法很简洁。 You can then after the DOM is loaded assign the functions to the controls. 然后,您可以在DOM加载后将功能分配给控件。 For example with jQuery you use: 例如,使用jQuery时,您可以使用:

<script type="text/javascript">
    $(document).ready(function(){
        $('#elementID').click(function(){
            // do stuff in here
        });
    });
</script>

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

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