Friday, 7 December 2012

Add Javascript and CSS dynamically from Content Page


Add Javascript and CSS dynamically from Content Page


Page class contains a public property named Header. You can convert HTML head tag to the corresponding server control by adding runat=”server” to its definition. Then you can access the head tag in code behind:


HtmlLink cssLink = new HtmlLink();

cssLink.Href = "~/css.css";

cssLink.Attributes.Add("rel", "stylesheet");

cssLink.Attributes.Add("type", "text/css");

Header.Controls.Add(cssLink);

 

HtmlLink jsLink = new HtmlLink();

jsLink.Href = "~/js.js";

jsLink.Attributes.Add("language", "javascript");

jsLink.Attributes.Add("type", "text/javascript");

Header.Controls.Add(jsLink);


Now run the application and view the html source of the page:


<link href="css.css" rel="stylesheet" type="text/css" />

<link href="js.js" language="javascript" type="text/javascript" />


References:



 
 

No comments:

Post a Comment

Thank You for Your Comments. We will get back to you soon.

back to top