user1977936
user1977936

Reputation: 31

Load js and css files in master page dynamic

i have an master page and i use it in a aspx page, when i press a button i want to load some javascript files and css files and load a user control to my page, so i am doing this:

ContentPlaceHolder headContent = (ContentPlaceHolder)this.Page.Master.FindControl("headerContent");

HtmlGenericControl control = new HtmlGenericControl();
control.TagName = "script";
control.Attributes.Add("type", "text/javascript");
control.Attributes.Add("src", ResolveUrl("Scripts/jquery-1.9.1.min.js"));

headContent.Controls.Add(control);

all right, but if i load some plugins they dont work. im loading this in user control oninit function.

i have a placeholder in master page head.

Upvotes: 0

Views: 944

Answers (1)

Seyed Morteza Mousavi
Seyed Morteza Mousavi

Reputation: 6963

Instead of manually creating script tag, just call the useful RegisterClientScriptInclude. It is easier. So change your code to following:

Page.ClientScript.RegisterClientScriptInclude("jQuery", "Scripts/jquery-1.9.1.min.js");

or If your going to resolve url, do this:

Page.ClientScript.RegisterClientScriptInclude("jQuery", ResolveUrl("Scripts/jquery-1.9.1.min.js"));

Upvotes: 1

Related Questions