在asp.net c#中运行时添加多个js文件

I want to add multiple .js file in my ASP.NET c# page at run time on button click.

Literal link2 = new Literal();
Literal link3 = new Literal();
Literal link4 = new Literal();

link2.Text = "<script src=\"Theme1/js/jquery-1.8.2.min.js\" type=\"text/javascript\" />";
link3.Text = "<script src=\"Theme1/js/supersized.3.2.7.min.js\" type=\"text/javascript\" />";
link4.Text = "<script src=\"Theme1/js/supersized-init.js\" type=\"text/javascript\" />";

base.Page.Header.Controls.Add(link2);
base.Page.Header.Controls.Add(link3);
base.Page.Header.Controls.Add(link4);
dongtui4038
dongtui4038 你有什么问题?从你的问题中不清楚你在问什么。另外,为什么php标签?
6 年多之前 回复

2个回答

The better way is to use RegisterStartupScript method:

http://msdn.microsoft.com/en-us/library/z9h4dk8y.aspx

EXAMPLE :

string jsBlock = "<script src='myJsFile.js'></script>";
if (!Page.ClientScript.IsStartupScriptRegistered("myJsInclude"))
   Page.ClientScript.RegisterStartupScript(typeof(string), "myJsInclude", jsBlock, false);

OR

And even better in your case RegisterClientScriptInclude:

http://msdn.microsoft.com/en-us/library/kx145dw2.aspx

EXAMPLE

if (!Page.ClientScript.IsClientScriptIncludeRegistered("myJsInclude"))
   Page.ClientScript.RegisterClientScriptInclude("myJsInclude", "myJsFile.js");



  HtmlLink css3 = new HtmlLink(); 
css3.Href =“theme2 / css / style.css”; \ n css3.Attributes [“rel”] =“stylesheet”;
css3.Attributes [“type”] =“text / css”;
css3.Attributes [“media”] =“all”;
Page。 Header.Controls.Add(css3);
</ code> </ pre>
</ div>

展开原文

原文

            HtmlLink css3 = new HtmlLink();
            css3.Href = "theme2/css/style.css";
            css3.Attributes["rel"] = "stylesheet";
            css3.Attributes["type"] = "text/css";
            css3.Attributes["media"] = "all";
            Page.Header.Controls.Add(css3);

Csdn user default icon
上传中...
上传图片
插入图片
抄袭、复制答案,以达到刷声望分或其他目的的行为,在CSDN问答是严格禁止的,一经发现立刻封号。是时候展现真正的技术了!
立即提问