Dynamically add a stylesheet file to a page

Last post 10-08-2007 1:40 AM by Jarmo. 8 replies.

Sort Posts:

  • Dynamically add a stylesheet file to a page

    10-06-2007, 9:46 AM
    • Member
      4 point Member
    • Jarmo
    • Member since 10-06-2007, 1:52 AM
    • Posts 21

    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 );
    	}
     Jarmo
  • Re: Dynamically add a stylesheet file to a page

    10-06-2007, 12:21 PM
    • Participant
      1,195 point Participant
    • eyad.salamin
    • Member since 05-19-2007, 5:59 PM
    • Posts 177

     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);
    }
      
    <%= 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>
  • Re: Dynamically add a stylesheet file to a page

    10-06-2007, 12:36 PM
    • Member
      4 point Member
    • Jarmo
    • Member since 10-06-2007, 1:52 AM
    • Posts 21

    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.

  • Re: Dynamically add a stylesheet file to a page

    10-06-2007, 1:11 PM
    • Participant
      1,195 point Participant
    • eyad.salamin
    • Member since 05-19-2007, 5:59 PM
    • Posts 177

     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);
       
    }

    <%= 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>
  • Re: Dynamically add a stylesheet file to a page

    10-06-2007, 1:11 PM
    Answer
    • Member
      4 point Member
    • Jarmo
    • Member since 10-06-2007, 1:52 AM
    • Posts 21

    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.

  • Re: Dynamically add a stylesheet file to a page

    10-06-2007, 1:18 PM
    • Participant
      1,195 point Participant
    • eyad.salamin
    • Member since 05-19-2007, 5:59 PM
    • Posts 177

    Hi,  

     

    Jarmo:
    Hi! I just tried Page.Header += "something"

     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!!

    <%= 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>
  • Re: Dynamically add a stylesheet file to a page

    10-06-2007, 1:18 PM
    • Member
      4 point Member
    • Jarmo
    • Member since 10-06-2007, 1:52 AM
    • Posts 21

    I tried Page.Header.InnerHtml += "something" again. I have many tags inside of head (master page). When I removed all entries, then it worked.

  • Re: Dynamically add a stylesheet file to a page

    10-06-2007, 2:23 PM
    • Participant
      1,195 point Participant
    • eyad.salamin
    • Member since 05-19-2007, 5:59 PM
    • Posts 177

    Jarmo:
    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>
  • Re: Dynamically add a stylesheet file to a page

    10-08-2007, 1:40 AM
    • Member
      4 point Member
    • Jarmo
    • Member since 10-06-2007, 1:52 AM
    • Posts 21

    Today I found that there is a HtmlLink class. In its documentation is an example how to dynamically add a stylesheet Smile See http://msdn2.microsoft.com/en-us/library/system.web.ui.htmlcontrols.htmllink.aspx

Page 1 of 1 (9 items)
Microsoft Communities