Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Dynamically adding js to asp.net file

In my Page_Load I identify the page header block from my master page and add javascript from an XML file to that part of the page.

It doesn't seem to be loading in time. If I explicitly put the script reference on the page, it works. But not when I load through the page_load event.

Should it be loaded sooner?

like image 248
Michael Warren Avatar asked Aug 06 '26 12:08

Michael Warren


1 Answers

You're not adding any code so I'm not able to tell you what you're doing wrong. Instead, I'll tell you a valid way to add script references from code behind.

Something like this on Page_Load should do the trick

var js = new HtmlGenericControl("script");
js.Attributes["type"] = "text/javascript";
js.Attributes["src"] = "myFile.js";
Page.Header.Controls.Add(js);
like image 190
Claudio Redi Avatar answered Aug 08 '26 01:08

Claudio Redi