control to add <LINK> inside <HEAD>

Discussion in 'ASP .Net Building Controls' started by Oleg Ogurok, Sep 10, 2003.

  1. Oleg Ogurok

    Oleg Ogurok Guest

    Hi all,

    I'm building a custom control, which has a separate .css file with its
    stylesheet. I need to link to the stylesheet inside the page, e.g.:
    <link rel="stylesheet" href="path/to/sheet.css" type="text/css">

    I understand according to HTML standards, I can only add a <link> element
    inside <HEAD>.
    Is there a method similar to Page.RegisterStartupScript() that emits text
    inside <HEAD> element rather than inside <BODY>?

    Thanks,

    Oleg.
    Oleg Ogurok, Sep 10, 2003
    #1
    1. Advertising

  2. Oleg Ogurok

    webgenie Guest

    here is a sample code

    in WebForm1.aspx

    <%@ Page Language="C#" Src="WebForm1.aspx.cs" %>
    <html>
    <head id="head1" runat="server"></head>
    <body></body>
    </html>


    in WebForm1.aspx.cs

    public class WebForm1 : System.Web.UI.Page {
    //variable for <HEAD> tag.
    //because of runat="server" attribute, <HEAD> tag become an instance of HtmlCotnainerControl class.
    protected System.Web.UI.HtmlControls.HtmlContainerControl head1;

    private void Page_Load(object sender, EventArgs e) {
    //declare string variable for <LINK> tag to add inside of <HEAD> tag
    string link = "<link rel=\"stylesheet\" type=\"text/css\" href=\"url_of_your_css_file\">";
    head1.Controls.Add(new LiteralControl(link));
    }
    }//end of class WebForm1
    webgenie, Sep 17, 2003
    #2
    1. Advertising

  3. Oleg Ogurok

    Oleg Ogurok Guest

    Re: here is a sample code

    Thanks, very impressive.
    Is there a way to modify the standard ASPX template in VS.NET to generate this code each time a user adds an ASPX file to a project?

    -Oleg.
    "webgenie" <> wrote in message news:...

    in WebForm1.aspx

    <%@ Page Language="C#" Src="WebForm1.aspx.cs" %>
    <html>
    <head id="head1" runat="server"></head>
    <body></body>
    </html>


    in WebForm1.aspx.cs

    public class WebForm1 : System.Web.UI.Page {
    //variable for <HEAD> tag.
    //because of runat="server" attribute, <HEAD> tag become an instance of HtmlCotnainerControl class.
    protected System.Web.UI.HtmlControls.HtmlContainerControl head1;

    private void Page_Load(object sender, EventArgs e) {
    //declare string variable for <LINK> tag to add inside of <HEAD> tag
    string link = "<link rel=\"stylesheet\" type=\"text/css\" href=\"url_of_your_css_file\">";
    head1.Controls.Add(new LiteralControl(link));
    }
    }//end of class WebForm1
    Oleg Ogurok, Sep 17, 2003
    #3
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Brian W
    Replies:
    10
    Views:
    749
    Brian W
    Jul 2, 2003
  2. showme

    PHP and ASP.NET go HEAD to HEAD

    showme, Jul 8, 2004, in forum: ASP .Net
    Replies:
    13
    Views:
    707
    Sherif ElMetainy
    Jul 10, 2004
  3. Karl
    Replies:
    0
    Views:
    382
  4. Replies:
    3
    Views:
    523
    Karl Seguin
    Aug 9, 2005
  5. Jiho Han

    Write into <HEAD></HEAD> section?

    Jiho Han, Jan 14, 2004, in forum: ASP .Net Building Controls
    Replies:
    6
    Views:
    217
    Sam Fields
    Jan 16, 2004
Loading...

Share This Page