How to Programmatically add JavaScript File to Asp.Net page?

27 09 2008

If you do not want to add your JavaScript files declaratively via HTML to your ASP.Net page, you can do it in code, and here is how:

Just add this code to the Page_Init event handler on your page:

protected void Page_Init(object sender, EventArgs e)


HtmlGenericControl js = new HtmlGenericControl(“script”);

js.Attributes[“type”] = “text/javascript”;

js.Attributes[“src”] = “jscript/formfunctions.js”;



And thats all there is to it




Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )


Connecting to %s

%d bloggers like this: