最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - How to ensure js is included in ascx file? - Stack Overflow

programmeradmin5浏览0评论

So the problem is the following :

i do have own user control. which has some javascript file in script tag included like this

<script type="text/javascript" src="../somefile.js" ></script>

or some javascript code directly on the ascx page. everything works if i place this control somewhere in aspx markup. but if i add this control to the page dynamically to some UpdatePanel place holder on postback (this is logic i can not alter), control renders itself and then i get js error message, which says that functions which are placed in somefile.js are not defined/are null. Why is this happening ? Is there anyway to force js including in this case.

sorry for being not enough specific but the code amount is huge and i was not able to provide a simplified example with the same error,.

The script is included but somehow the functions are not defined. I'm novice to js, so is it possible that just the script is included but not executed, so the functions are not declared ???

The interesting is that if on some page my custom control is declared in aspx. The adding some more instances dynamically causes no problem.

So the problem is the following :

i do have own user control. which has some javascript file in script tag included like this

<script type="text/javascript" src="../somefile.js" ></script>

or some javascript code directly on the ascx page. everything works if i place this control somewhere in aspx markup. but if i add this control to the page dynamically to some UpdatePanel place holder on postback (this is logic i can not alter), control renders itself and then i get js error message, which says that functions which are placed in somefile.js are not defined/are null. Why is this happening ? Is there anyway to force js including in this case.

sorry for being not enough specific but the code amount is huge and i was not able to provide a simplified example with the same error,.

The script is included but somehow the functions are not defined. I'm novice to js, so is it possible that just the script is included but not executed, so the functions are not declared ???

The interesting is that if on some page my custom control is declared in aspx. The adding some more instances dynamically causes no problem.

Share Improve this question edited Feb 17, 2011 at 4:11 danyloid asked Feb 17, 2011 at 3:18 danyloiddanyloid 1,6773 gold badges21 silver badges48 bronze badges
Add a comment  | 

3 Answers 3

Reset to default 17

In the load event of your control, do something like this:

if (!Page.ClientScript.IsClientScriptIncludeRegistered("keyName"))
    Page.ClientScript.RegisterClientScriptInclude("keyName",
        Page.ResolveUrl("~/Scripts/MyJavaScript.js"));

How about including it in ScriptManager.Scripts like this?

<asp:ScriptManager runat="server" ID="scriptManager1" EnablePageMethods="true">
        <Scripts>
            <asp:ScriptReference Path="~/somefile.js" />
        </Scripts>
</asp:ScriptManager>

Edit:

From ascx you could do something like this (OnPreRender would be where I would try this):

ScriptManager.GetCurrent(Page).Scripts.Add(new ScriptReference("~/somefile.js"));

Alternatively, you could try

 ScriptManager.GetCurrent(Page).RegisterClientScriptInclude(
            this,
            typeof(Page),
            "UniqueScriptKey",
            ResolveClientUrl("~/somefile.js"));

Reference link

Are you sure your JS file is loading? It could be that the file is not in the right location - don't use ../ in the path as this is relative and may be wrong if you are sharing this and the pages are not all at the same depth:

do something like this:

<script type="text/javascript" src="/Scripts/somefile.js" ></script>
发布评论

评论列表(0)

  1. 暂无评论