English 中文(简体)
如何确保js包含在ascx文件中?
原标题:How to ensure js is included in ascx file?

因此,问题如下:

我确实有自己的用户控制。它在script标签中包含了一些javascript文件,如下所示

<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.

很抱歉没有足够具体,但代码量很大,我无法提供一个具有相同错误的简化示例,。

包含了脚本,但不知何故,没有定义函数。我是js的新手,所以有没有可能只包含了脚本而没有执行,所以函数没有声明???

有趣的是,如果在某个页面上,我的自定义控件是用aspx声明的。动态添加更多实例不会造成问题。

最佳回答

在控件的加载事件中,执行以下操作:

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

把它包含在ScriptManager中怎么样。像这样的脚本?

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

编辑:

从ascx,你可以做这样的事情(OnPreRender将是我尝试的地方):

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

或者,你可以试试

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

参考链接

你确定你的JS文件正在加载吗?可能是文件位置不对-不要使用../在路径中,因为这是相对的,如果您共享这一点,并且页面的深度不完全相同,则可能是错误的:

这样做:

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




相关问题
Anyone feel like passing it forward?

I m the only developer in my company, and am getting along well as an autodidact, but I know I m missing out on the education one gets from working with and having code reviewed by more senior devs. ...

How to Add script codes before the </body> tag ASP.NET

Heres the problem, In Masterpage, the google analytics code were pasted before the end of body tag. In ASPX page, I need to generate a script (google addItem tracker) using codebehind ClientScript ...

Transaction handling with TransactionScope

I am implementing Transaction using TransactionScope with the help this MSDN article http://msdn.microsoft.com/en-us/library/system.transactions.transactionscope.aspx I just want to confirm that is ...

System.Web.Mvc.Controller Initialize

i have the following base controller... public class BaseController : Controller { protected override void Initialize(System.Web.Routing.RequestContext requestContext) { if (...

Microsoft.Contracts namespace

For what it is necessary Microsoft.Contracts namespace in asp.net? I mean, in what cases I could write using Microsoft.Contracts;?

Separator line in ASP.NET

I d like to add a simple separator line in an aspx web form. Does anyone know how? It sounds easy enough, but still I can t manage to find how to do it.. 10x!

热门标签