I am using one style sheet on master page so all pages use the same style. I am writing one specific page which is used very seldom. I use specific styles on this page. I did not want to add the styles needed in this page to main style sheet, so I decided
to write a separate .css. OK, I did it, but then I had a problem how to "insert" link tag under head tag. I found a nice way. On my page's OnInit, I just add the literal to controls of page header.
protected override void OnInit( EventArgs e )
{
Literal link = new Literal();
link.Text = "<link href=\"ContentEditor.css\" rel=\"stylesheet\" type=\"text/css\" />";
base.Page.Header.Controls.Add( link );
base.OnInit( e );
}
Thanks! I tried this.Header.InnerHtml, but it did not work. Why? I tried to get html, append my tag and set it. this.Header.InnerHtml = "something" worked, but this.Header.InnerHtml = this.Header.InnerHtml + "something" failed. InnerHtml's property get generated
an exception.
Hi! I just tried Page.Header.InnerHtml += "something", but it threw an exception "Cannot get inner content of because the contents are not literal.", which is the same as I got before. So, my initial message has the working solution.
I tried Page.Header.InnerHtml += "something" again. I have many tags inside of head (master page). When I removed all entries, then it worked.
You are right! I tried to add a tag to the head tag and it gave me the exception you mentioned.
I'm not sure, but I think this is a bug.
I tried to render the contents of the header and i was able to get the rendered html:
StringBuilder sb = new StringBuilder();
System.IO.StringWriter tw = new System.IO.StringWriter(sb);
HtmlTextWriter hw = new HtmlTextWriter(tw);
this.Header.RenderControl(hw);
string headerRenderedHtml = sb.ToString();
Response.Write(Server.HtmlEncode(headerRenderedHtml));
no exceptions and the html was rendered..
isn't it strange? i googled around looking for something with no luck. anyways, since your solution works fine then there's nothing to worry about. thanks for sharing it by the way.
<%=Eyad.Salamin%>
<aspForums:DontForget>Don't forget to click on Mark as answer on the post that helped you.
This credits that member, earns you a point and marks your thread as Resolved so we will all know you have been helped. </aspForums:DontForget>
I want dynamically add a link element to my page but I need to do that from a Class libary (not a .aspx page). All the pages in the application calls this class function so i need to add the dynamic css code in that function. But I am not able to access
the Page class for the below code
Jarmo
Member
4 Points
21 Posts
Dynamically add a stylesheet file to a page
Oct 06, 2007 01:46 PM|LINK
Hi,
I am using one style sheet on master page so all pages use the same style. I am writing one specific page which is used very seldom. I use specific styles on this page. I did not want to add the styles needed in this page to main style sheet, so I decided to write a separate .css. OK, I did it, but then I had a problem how to "insert" link tag under head tag. I found a nice way. On my page's OnInit, I just add the literal to controls of page header.
protected override void OnInit( EventArgs e ) { Literal link = new Literal(); link.Text = "<link href=\"ContentEditor.css\" rel=\"stylesheet\" type=\"text/css\" />"; base.Page.Header.Controls.Add( link ); base.OnInit( e ); }eyad.salamin
Participant
1195 Points
177 Posts
Re: Dynamically add a stylesheet file to a page
Oct 06, 2007 04:21 PM|LINK
Nice.
Here's another way (without literal):
protected override void OnInit( EventArgs e ) { this.Header.InnerHtml += "<link type=\"text/css\" rel=\"Stylesheet\" href=\"styleSheet.css\" />"; base.OnInit(e); }<aspForums:DontForget>Don't forget to click on Mark as answer on the post that helped you.
This credits that member, earns you a point and marks your thread as Resolved so we will all know you have been helped. </aspForums:DontForget>
Jarmo
Member
4 Points
21 Posts
Re: Dynamically add a stylesheet file to a page
Oct 06, 2007 04:36 PM|LINK
Thanks! I tried this.Header.InnerHtml, but it did not work. Why? I tried to get html, append my tag and set it. this.Header.InnerHtml = "something" worked, but this.Header.InnerHtml = this.Header.InnerHtml + "something" failed. InnerHtml's property get generated an exception.
eyad.salamin
Participant
1195 Points
177 Posts
Re: Dynamically add a stylesheet file to a page
Oct 06, 2007 05:11 PM|LINK
what exactly is the exception?
I'm using the following code and it works perfectly:
protected override void OnInit( EventArgs e )
{
this.Header.InnerHtml = this.Header.InnerHtml + "<link type=\"text/css\" rel=\"Stylesheet\" href=\"styleSheet.css\" />";
base.OnInit(e);
}
<aspForums:DontForget>Don't forget to click on Mark as answer on the post that helped you.
This credits that member, earns you a point and marks your thread as Resolved so we will all know you have been helped. </aspForums:DontForget>
Jarmo
Member
4 Points
21 Posts
Re: Dynamically add a stylesheet file to a page
Oct 06, 2007 05:11 PM|LINK
Hi! I just tried Page.Header.InnerHtml += "something", but it threw an exception "Cannot get inner content of because the contents are not literal.", which is the same as I got before. So, my initial message has the working solution.
eyad.salamin
Participant
1195 Points
177 Posts
Re: Dynamically add a stylesheet file to a page
Oct 06, 2007 05:18 PM|LINK
Hi,
It's not Page.Header += "something";
it's:
this.Header .InnerHtml += "<link type=\"text/css\" rel=\"Stylesheet\" href=\"styleSheet.css\" />";
I tried it and it works very fine!!
<aspForums:DontForget>Don't forget to click on Mark as answer on the post that helped you.
This credits that member, earns you a point and marks your thread as Resolved so we will all know you have been helped. </aspForums:DontForget>
Jarmo
Member
4 Points
21 Posts
Re: Dynamically add a stylesheet file to a page
Oct 06, 2007 05:18 PM|LINK
I tried Page.Header.InnerHtml += "something" again. I have many tags inside of head (master page). When I removed all entries, then it worked.
eyad.salamin
Participant
1195 Points
177 Posts
Re: Dynamically add a stylesheet file to a page
Oct 06, 2007 06:23 PM|LINK
You are right! I tried to add a tag to the head tag and it gave me the exception you mentioned.
I'm not sure, but I think this is a bug.
I tried to render the contents of the header and i was able to get the rendered html:
StringBuilder sb = new StringBuilder();
System.IO.StringWriter tw = new System.IO.StringWriter(sb);
HtmlTextWriter hw = new HtmlTextWriter(tw);
this.Header.RenderControl(hw);
string headerRenderedHtml = sb.ToString();
Response.Write(Server.HtmlEncode(headerRenderedHtml));
no exceptions and the html was rendered..
isn't it strange? i googled around looking for something with no luck. anyways, since your solution works fine then there's nothing to worry about. thanks for sharing it by the way.
<aspForums:DontForget>Don't forget to click on Mark as answer on the post that helped you.
This credits that member, earns you a point and marks your thread as Resolved so we will all know you have been helped. </aspForums:DontForget>
Jarmo
Member
4 Points
21 Posts
Re: Dynamically add a stylesheet file to a page
Oct 08, 2007 05:40 AM|LINK
Today I found that there is a HtmlLink class. In its documentation is an example how to dynamically add a stylesheet [:)] See http://msdn2.microsoft.com/en-us/library/system.web.ui.htmlcontrols.htmllink.aspx
tahernd
Member
16 Points
9 Posts
Re: Dynamically add a stylesheet file to a page
May 31, 2010 11:57 AM|LINK
Hello,
I want dynamically add a link element to my page but I need to do that from a Class libary (not a .aspx page). All the pages in the application calls this class function so i need to add the dynamic css code in that function. But I am not able to access the Page class for the below code
Please suggest !
ASP.NET Dynamic Css
Taher
http://tdtechdiary.blogspot.com