1

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);
1
  • 1
    What are you having trouble with? It's not clear from your question what you are asking. Also, why the php tag? Commented Feb 18, 2014 at 6:42

2 Answers 2

1

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");
Sign up to request clarification or add additional context in comments.

Comments

1
            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);

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.