How to add link to a css file in content page?

Last post 12-02-2009 8:16 PM by jaimirg. 7 replies.

Sort Posts:

  • How to add link to a css file in content page?

    06-11-2008, 4:11 PM
    • Participant
      1,875 point Participant
    • aspfun
    • Member since 04-15-2004, 9:05 PM
    • Posts 769

    How to add link to a css file in content page?

    There is no Head tag.

  • Re: How to add link to a css file in content page?

    06-11-2008, 5:03 PM
    Answer
    • Contributor
      2,255 point Contributor
    • benrick
    • Member since 07-27-2006, 3:12 PM
    • Kent, Ohio
    • Posts 379
    • TrustedFriends-MVPs

     Try this

    In VB 

      

    Protected Sub Page_Init(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Init
        Dim css As HtmlGenericControl
        css = New HtmlGenericControl
        css.TagName = "style"
        css.Attributes.Add("type", "text/css")
        css.InnerHtml = "@import ""/foobar.css"";"
        Page.Header.Controls.Add(css)
    End Sub
     

    In C#

      

    protected void Page_Init(object sender, System.EventArgs e)
    {
        HtmlGenericControl css;
        css = new HtmlGenericControl();
        css.TagName = "style";
        css.Attributes.Add("type", "text/css");
        css.InnerHtml = "@import \"/foobar.css\";";
        Page.Header.Controls.Add(css);
    }
     

    I hope that helps,
    Brendan 

    C. Brendan Enrick
    Brendan's Blog

    Make sure you click "Mark as Answer" for any post which has helped you. This will give recognition to those helping others as well as earn you a point. It also helps people know which posts still need work.
    Filed under:
  • Re: How to add link to a css file in content page?

    06-11-2008, 11:20 PM
    Answer
    • All-Star
      30,502 point All-Star
    • HeartattacK
    • Member since 01-08-2007, 5:53 PM
    • Dhaka, Bangladesh
    • Posts 3,292
    • Moderator

    Masterpage:

     

    <%@ Master Language="C#" AutoEventWireup="true" CodeBehind="Site1.master.cs" Inherits="webservicelogintest.Site1" %>
    
    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
    
    <html xmlns="http://www.w3.org/1999/xhtml" >
    <head runat="server">
        <title>Untitled Page</title>
        <asp:ContentPlaceHolder ID="head" runat="server">
        </asp:ContentPlaceHolder>
    </head>
    <body>
        <form id="form1" runat="server">
        <div>
            <asp:ContentPlaceHolder ID="ContentPlaceHolder1" runat="server">
            
            </asp:ContentPlaceHolder>
        </div>
        </form>
    </body>
    </html>

     

     

    ContentPage:

     

    <%@ Page Language="C#" MasterPageFile="~/Site1.Master" AutoEventWireup="true" CodeBehind="WebForm1.aspx.cs" Inherits="webservicelogintest.floder.WebForm1" Title="Untitled Page" %>
    <asp:Content ID="Content1" ContentPlaceHolderID="head" runat="server">
        <link ... />
    </asp:Content>
    <asp:Content ID="Content2" ContentPlaceHolderID="ContentPlaceHolder1" runat="server">
        <input type='text' />
        <input type='text'  />
    </asp:Content>

     

    That's right, you can place a content placeholder in the head :)

    In VS 2005, it'll show an error (squigly line under the content placeholder tag). Don't worry, it'll compile and run fine. In VS 2008, it doesn't even give that warning.

    All that glitters is gold-
    Only shooting stars break the mold.

    Read my blog: www.heartysoft.com

    Tell me what tutorials / articles / videos you want to see on my site.
  • Re: How to add link to a css file in content page?

    06-12-2008, 12:34 AM

    Hi Try this,

    Add link inside the ContentPlaceHolder Tag as below code..

    <asp:Content ID="cphRequestForm" ContentPlaceHolderID="cphCommbuilder" runat="server">

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

    Your HTML goes here...

    Hope this helps.

    If this post helps you, Click Mark as Answer

  • Re: How to add link to a css file in content page?

    06-12-2008, 1:34 AM
    Answer
    • All-Star
      30,502 point All-Star
    • HeartattacK
    • Member since 01-08-2007, 5:53 PM
    • Dhaka, Bangladesh
    • Posts 3,292
    • Moderator

    @alsakaf:

    you should avoid putting the css link in he body (as will happen if you put the link in the contentplaceholder where all the other html goes). You should put a contentplaceholder in the head of the masterpage and in the contentpage, put the link in the content for the head contentplaceholder. That's the recommended practice, straight from scott gu's blog :)

    [btw...that's what my code above does]

    All that glitters is gold-
    Only shooting stars break the mold.

    Read my blog: www.heartysoft.com

    Tell me what tutorials / articles / videos you want to see on my site.
  • Re: How to add link to a css file in content page?

    06-12-2008, 9:09 AM
    • Member
      356 point Member
    • krishnav
    • Member since 05-01-2008, 9:45 AM
    • Vijayawada
    • Posts 80

    <link rel="stylesheet" src="path of the stylesheet" type="text/css">

    just add the above line in between the head tag.

  • Re: How to add link to a css file in content page?

    06-12-2008, 9:41 AM
    • Participant
      1,875 point Participant
    • aspfun
    • Member since 04-15-2004, 9:05 PM
    • Posts 769

    I tested Brendan's way, it works fine.

    Thank all of you.

  • Re: How to add link to a css file in content page?

    12-02-2009, 8:16 PM
    • Member
      2 point Member
    • jaimirg
    • Member since 12-02-2009, 8:14 PM
    • Colombia
    • Posts 1

    This is an other way

    protected void Page_Init(object sender, EventArgs e)
            {
                HtmlLink css1 = new HtmlLink();
                css1.Href = "css/fancyforms.css";
                css1.Attributes["rel"] = "stylesheet";
                css1.Attributes["type"] = "text/css";
                css1.Attributes["media"] = "all";
                Page.Header.Controls.Add(css1);
            }

    Jaimir G.
    http://msmvps.com/blogs/jaimirg/

Page 1 of 1 (8 items)