|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Announcements
Chapters
Services
Feature Zones
|
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
IntroductionSome times you want to dynamically add a javascript to the header of page or even dynamically add a stylesheet to yuor page. BackgroundWhen 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 codeThe 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
|
|||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||