Add JavaScript reference from code behind (C#)

前端 未结 3 999
耶瑟儿~
耶瑟儿~ 2021-02-08 12:24

Is it possible to add javascript reference dynamically from code behind aspx.cs?

Like this:

private void AddScriptReference(string path)
{
   //Add refer         


        
3条回答
  •  遥遥无期
    2021-02-08 13:01

    For those who want to know the syntax, here it is:

    Master Page:

    
    

    Code behind:

    ScriptReference sr = new ScriptReference("path-to-js.js");
    ScriptManager sm = (ScriptManager)this.Master.FindControl("ScriptManager");
    sm.Scripts.Add(sr);
    

    Or:

    ScriptManager.RegisterClientScriptInclude(this.Page, GetType(), "UniqueID", "path-to-js.js");
    

    But none of these solutions actually add the script to the head of the page..

提交回复
热议问题