Thursday 3 May 2012

Dynamically adding a CSS link to the page in c#


The following code adds a CSS link to the header node of a Html page.

C#
===
// Register CSS Link
HtmlHead head = (HtmlHead)Page.Header;
HtmlLink link = new HtmlLink();
link.Attributes.Add("href", Page.ResolveClientUrl("~/HTMLEditorImages/Custom.css"));
//if above line code causes to error like (the database is not accessible. (objectexplorer)) then
//Add like bellow line code.
 //link.Attributes.Add("href", Page.ResolveClientUrl("HTMLEditorImages/Custom.css"));
 link.Attributes.Add("type""text/css");
link.Attributes.Add("rel""stylesheet");
head.Controls.Add(link); 
 
VB.Net
=======
// Register CSS Link
Dim head As HtmlHead = DirectCast(Page.Header, HtmlHead)
Dim link As New HtmlLink()
link.Attributes.Add("href", Page.ResolveClientUrl("~/HTMLEditorImages/Custom.css"))
//if above line code causes to error like (the database is not accessible. (objectexplorer)) then
//Add like bellow line code. 
//link.Attributes.Add("href", Page.ResolveClientUrl("HTMLEditorImages/Custom.css"))
 link.Attributes.Add("type", "text/css")
link.Attributes.Add("rel", "stylesheet")
head.Controls.Add(link)
 
 
 
OutPut
------
 
 
 
By abserving the source of the page we can identified that 
<link href"HTMLEditorImages/Custom.css" type="text/css" rel="stylesheet" />
is added at the end of the All Links with in the <Head> .....</Head> .


For Reference.....

No comments:

Post a Comment