开发者

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. Inside this js file let's say I have a function called 'MyFunction'. In the onclick event of a div I'm wanting to call that function and I am unable to. 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:

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

Now, after this, I'm wondering, is there anything special I need开发者_Go百科 to do to be able to call a function that's in the include file? like in my div:

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


i use the 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. Al W's method above is neat. You can then after the DOM is loaded assign the functions to the controls. For example with jQuery you use:

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

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜