5,699,997 members and growing! (23,187 online)
Email Password   helpLost your password?
Web Development » ASP.NET » Howto     Beginner License: The Code Project Open License (CPOL)

How to add javascript or stylesheet to header of asp.net page?

By Armin Kabir

This article will show you how to dynamically add javascript or stylesheet to the header of asp.net page.
C# (C# 2.0, C# 3.0, C#), VB (VB 8.0, VB 9.0, VB), Javascript, CSS, HTML, Ajax, ASP.NET, Dev, Design

Posted: 9 Apr 2008
Updated: 9 Apr 2008
Views: 8,178
Bookmarked: 4 times
Announcements
Loading...



Search    
Advanced Search
Sitemap
18 votes for this Article.
Popularity: 1.76 Rating: 1.40 out of 5
2 votes, 33.3%
1
0 votes, 0.0%
2
1 vote, 16.7%
3
1 vote, 16.7%
4
2 votes, 33.3%
5
Note: This is an unedited contribution. If this article is inappropriate, needs attention or copies someone else's work without reference then please Report This Article

Introduction

Some times you want to dynamically add a javascript to the header of page or even dynamically add a stylesheet to yuor page.

Background

When we are using Page.ClientScript functions, the scripts won't be added to the header it is going to be added to different part of page.

Using the code

The following code shows how you can dynamically add script or stylesheet or any other type of control to the header of page.

	'Add the css to header
        Dim hControl As LiteralControl = New LiteralControl
        hControl.Text = "<link href=""default.css"" type=""text/css""  
        rel=""stylesheet"" />"
        Me.Page.Header.Controls.Add(hControl)
        'Add javascript for the header
        Dim header As LiteralControl = New LiteralControl
        header.Text = "<script type=""text/javascript""   
        src=""EWNHeader.js""></script>"
        Me.Page.Header.Controls.Add(header)
	

Here is the new version of my code which i got it from comments (nice job Matteo) In the following code you can avoid using literal: To add a meta tag:

HtmlMeta metadescription = new HtmlMeta();
metadescription.Name = "description";
metadescription.Content = "Your page description here";
Page.Header.Controls.Add(metadescription);
To add a stylesheet:
HtmlLink css = new HtmlLink(); 
css.Href = "mystyle.css";
css.Attributes["rel"] = "stylesheet";
css.Attributes["type"] = "text/css";
css.Attributes["media"] = "all";
Page.Header.Controls.Add(css);
To add an external javascript reference:
HtmlGenericControl js = new HtmlGenericControl("script");
js.Attributes["type"] = "text/javascript";
js.Attributes["src"] = "mylibrary.js";
Page.Header.Controls.Add(js);
Thanks again Matteo;)

Points of Interest

As you can see first we define a LiteralControl and then we assign our javascript or stylesheet to the text properties of LiteralControl and then we will add the LiteralControl to header of the page. Now when you run the page, you will see the javascript or stylesheet has been added to the header. you can call put all this code in afunctiona nd call it on the page_load of asp.net page.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

About the Author

Armin Kabir



Occupation: Software Developer (Senior)
Company: Innovative Design
Location: United States United States

Other popular ASP.NET articles:

Article Top
Sign Up to vote for this article
You must Sign In to use this message board.
FAQ FAQ Noise ToleranceSearch Search Messages 
 Layout  Per page   
 Msgs 1 to 6 of 6 (Total in Forum: 6) (Refresh)FirstPrevNext
GeneralMaster PagesmemberMember 30555643:09 4 Nov '08  
GeneralAvoid LiteralmemberMatteo Casati6:58 9 Apr '08  
GeneralRe: Avoid LiteralmemberArmin Kabir7:56 9 Apr '08  
GeneralRe: Avoid LiteralmemberMatteo Casati8:03 9 Apr '08  
GeneralRe: Avoid LiteralmemberArmin Kabir8:53 9 Apr '08  
GeneralRe: Avoid LiteralmemberAndreyMir9:07 10 Apr '08  

General General    News News    Question Question    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

PermaLink | Privacy | Terms of Use
Last Updated: 9 Apr 2008
Editor:
Copyright 2008 by Armin Kabir
Everything else Copyright © CodeProject, 1999-2008
Web10 | Advertise on the Code Project