簡體   English   中英

在ascx控件中或在父頁面上定義javascript?

[英]define javascript in ascx control or on parent page?

我遇到了無法正常運行的javascript問題。 我的asp.net 3.5 webforms應用程序有一個頁面,其中包含幾個包含javascript的用戶控件(ascx)。 並非所有人都能正常工作放入javascript的最佳做法是什么? 在父頁面的頂部還是在用戶控件中? 我也總是需要這樣定義嗎? 我在這里不使用任何js框架:

window.onload = function(){ 
//initialize js scripts

}

我將在控件中使用RegisterStartupScript ,並讓ASP.NET為您工作。 另外,如果您在頁面上具有同一控件的多個實例,這將避免很多問題:

    String csname1 = "PopupScript";
    Type cstype = this.GetType();

    // Get a ClientScriptManager reference from the Page class.
    ClientScriptManager cs = Page.ClientScript;

    // Check to see if the startup script is already registered.
    if (!cs.IsStartupScriptRegistered(cstype, csname1))
    {    
        cs.RegisterStartupScript(cstype, csname1, "alert('Hello World!')", true);
    }

暫無
暫無

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

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