簡體   English   中英

如何僅將UserControl中的javascript文件添加到ASP.NET頁一次?

[英]How do I add a javascript file from a UserControl to an ASP.NET page only once?

我有一個需要javascript文件的ASP.NET UserControl。 這是一個jQuery插件。 我只想在頁面上使用UserControl時添加此文件。 因為它是一個jQuery插件,所以我需要在添加jQuery文件后添加它。

由於存在母版頁等內容,因此當我使用RegisterClientScriptInclude ,腳本會在jQuery腳本之前添加,因此無法正常工作。

RegisterStartupScript可以解決問題,但似乎無法與javascript文件一起使用(如果您提供實際的javascript代碼,則可以)。

而且,如果我將腳本兩次添加到頁面中,則我不希望控件兩次添加腳本。

看來這一定是微不足道的,但我無法使其正常工作。

RegisterStartupScript也可以注冊js文件。

var clientScriptManager = Page.ClientScript;

if(!clientScriptManager.IsStartupScriptRegistered("a_plugin"))
{
    clientScriptManager.RegisterStartupScript(Page.GetType(), "a_plugin", "<script src=\"/js/a_plugin.js\"></script>");
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM