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
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.
"If I can see further than anyone else, it is only because I am standing on the shoulders of giants."blog: www.heartysoft.com twitter: @ashic
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]
"If I can see further than anyone else, it is only because I am standing on the shoulders of giants."blog: www.heartysoft.com twitter: @ashic
Marked as answer by HeartattacK on Dec 03, 2009 10:01 AM
After link a css style file using any of the mechanisms described here, you can use styles defined in this file to apply it on any visual element on your web page.
aspfun
Participant
1918 Points
1083 Posts
How to add link to a css file in content page?
Jun 11, 2008 08:11 PM|LINK
How to add link to a css file in content page?
There is no Head tag.
benrick
Contributor
2439 Points
405 Posts
ASPInsiders
Re: How to add link to a css file in content page?
Jun 11, 2008 09:03 PM|LINK
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 SubIn C#
I hope that helps,
Brendan
CSS
Brendan's Blog
HeartattacK
All-Star
55262 Points
5917 Posts
Moderator
MVP
Re: How to add link to a css file in content page?
Jun 12, 2008 03:20 AM|LINK
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.
blog: www.heartysoft.com
twitter: @ashic
mohamed.alsa...
Participant
866 Points
134 Posts
Re: How to add link to a css file in content page?
Jun 12, 2008 04:34 AM|LINK
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
HeartattacK
All-Star
55262 Points
5917 Posts
Moderator
MVP
Re: How to add link to a css file in content page?
Jun 12, 2008 05:34 AM|LINK
@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]
blog: www.heartysoft.com
twitter: @ashic
krishnav
Member
356 Points
80 Posts
Re: How to add link to a css file in content page?
Jun 12, 2008 01:09 PM|LINK
<link rel="stylesheet" src="path of the stylesheet" type="text/css">
just add the above line in between the head tag.
aspfun
Participant
1918 Points
1083 Posts
Re: How to add link to a css file in content page?
Jun 12, 2008 01:41 PM|LINK
I tested Brendan's way, it works fine.
Thank all of you.
jaimirg
Member
20 Points
5 Posts
Re: How to add link to a css file in content page?
Dec 03, 2009 12:16 AM|LINK
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/
ashish.patya...
Member
2 Points
1 Post
How to add link to a css file in content page?
May 31, 2010 12:47 PM|LINK
bt sir how we can use css other coding in asp.net.
means after that link what is the next step.
please provide full information of this code.
thanks!!!!!
css coding
jaimirg
Member
20 Points
5 Posts
Re: How to add link to a css file in content page?
Jun 01, 2010 01:52 PM|LINK
Hi
After link a css style file using any of the mechanisms described here, you can use styles defined in this file to apply it on any visual element on your web page.
Example:
Content file css:
.header { position: relative; margin: 0px; padding: 0px; background: #4b6c9e; width: 100%; }Direct use in the markup:
Use in server code: