Update
I had the syntax incorrect ~/Styles/Default.css needed to be /Styles/Default.css
I have a CSS page linked to the master.
Now I created my Default.aspx page that uses the Master Page
In Default.aspx
<asp:Content ID="ContentHead" ContentPlaceHolderID="head" runat="server">
<link rel="stylesheet" runat="server" href="~/Styles/Default.css" type="text/css"/>
</asp:Content>
<asp:Content ID="ContentBanner" ContentPlaceHolderID="banner" runat="server">
<div id="banner">
<img src="/Images/Banners/banner.gif" />
</div>
</asp:Content>
Default.css
#banner
{
background-color:#FDFD82;
}
In VS I see the background color of that div correctly but when I load it in IE it doesn't show.
If I code the style into the div section and not CSS it works.
What I'm I missing here?
visual studio reloads the style sheet, but the browser doesnt, it caches it once and uses it unless you reload it.
so, either press Ctrl + R in your browser to refresh it and have it reload all the resources, or use a trick which will make the browser reload the style sheet every time:
<link rel="stylesheet" runat="server" href="~/Styles/Default.css?<%=DateTime.Now %>" type="text/css"/>
have a look at the link, i appended a query string with the current time. the time changes always, and it makes the browser think its a different sheet, and reload it every time.
Related
I am struggling to implement an external CSS file in the content pages of my asp.net WebForms application. The content page thus have a master page (Site.Master) in which I included the following code in the head section:
<asp:ContentPlaceHolder ID="HeadContent" runat="server">
<link href="Styles/pokerpsych.css" rel="stylesheet" type="text/css" />
</asp:ContentPlaceHolder>
The asp:ContentPlaceholder tag thus contains the link to the CSS file that I want to apply to all pages. The styling in this link is thus successfully applied to the content in the body tag of the Site.Master page however the styles specified in this file is not applied to IDs that are specified for content pages. In one of my content pages I have for instance the following code:
<%# Page Title="" Language="C#" MasterPageFile="~/Site.Master" AutoEventWireup="true" CodeBehind="PostHand.aspx.cs" Inherits="PokerPuzzleWebforms.PostHand" %>
<asp:Content ID="Content1" ContentPlaceHolderID="MainContent" runat="server">
<header>
<h1>Post your hand here</h1>
</header>
<main>
<label runat="server" for="titleBox" id="titleLabel" >Title</label>
<input type="text" id="titleBox" name="titleBox" runat="server" />
</main>
</asp:Content>
I am thus trying to apply styling to the label for the textbox (id="titleLabel") although I have not been successful.
I have validated the CSS file and there seems to be no errors. I have also tried to override the pokerpsych.css with another external CSS file by adding the following code in the content page:
<asp:Content ID="Head" ContentPlaceHolderID="HeadContent" runat="server">
<link href="Styles/posthandstyle.css" rel="stylesheet" type="text/css" runat="server"/>
</asp:Content>
I also tried embedding a style tag within the asp:Content tag although this was not successful. I am however only able to apply the styles to the id when I make use of inline styles although I would prefer to include an external file to apply the styles.
Below is the CSS file (pokerpsych.css):
body {
}
#test{
color:red;
}
#titleLabel {
color:red !important;
margin-right:100px;
}
From user1429080's comment:
This is probably caused by dynamic ID handling in Web Forms. The ID of the textbox in the rendered HTML is likely different from just titleLabel. Check by View source on the rendered page.
The easiest workaround might be to use class-based CSS and refer to the correct class on the control in the ASPX page instead of the IDs.
I have a master page and a content page for my ASP.NET web forms site. Both pages have style blocks.
The following is from my content page:
<asp:Content ID="Content1" ContentPlaceHolderID="HeadContent" runat="server">
<style type="text/css" runat="server">
.scroll td:nth-of-type(1)::before {
content: "Column Name";
color: black;
} ...
And the master page:
<head id="Head1">
<style type="text/css" runat="server">
...
</style>
...
<asp:ContentPlaceHolder runat="server" ID="HeadContent" /> ...
The rendered HTML only applies the content style block under two conditions: page refresh and disabling jQuery Mobile. Everything else seems to make no difference, and I have been unsuccessful at getting the style block to apply changes on the initial page load on the content page without disabling jQuery Mobile.
The style block is present at all times, but is not normally applied. I have tried to move the style to the body. I have deleted the master page's style block. I have experimented with runat="server". Also, the order of the content placeholder in relation to the master page's style block doesn't matter. Style from the master page is always rendered. I have compared the rendered source code before and after a page refresh and the rendered HTML pages pre and post refresh are identical despite the fact that only the refreshed page works as anticipated.
Is there something that I'm missing? I am using ASP.NET v4.5 Web Forms and Visual Studio 2012 update 4?
Question is pretty well stated in the title. Normally I would use <link... /> to reference my CSS Sheet but since I'm using a master page I don't have access to the Head Tag so how do I reference a specific CSS sheet on my ASPX page. I tried using <%# Import Namespace="Style.css" but no luck. Thanks for the help.
Just add a CSS ContentPlaceHolder with a default value in it.
Basically, the CSS file you specify as default will be included unless you override that placeholder with an tag from a child page.
Your Master Page should look something like this.
<head>
<asp:ContentPlaceHolder ID="Stylesheets" runat="server">
<link rel="stylesheet" href="/css/master.css" type="text/css" />
</asp:ContentPlaceHolder>
</head>
Then from any pages using that Master Page, you can simply override that with a different stylesheet.
On (example) AboutUs.aspx
<asp:Content ID="Content1" ContentPlaceHolderID="Stylesheets" runat="server">
<link rel="stylesheet" href="/css/Style.css" type="text/css" />
</asp:Content>
If you want to add a CSS stylesheet to any ASPX page, you should use PlaceHolders.
Master page: (in the section)
<asp:ContentPlaceHolder ID="HeadContent" runat="server">
</asp:ContentPlaceHolder>
ASPX Page:
<asp:Content ID="HeaderContent" runat="server" ContentPlaceHolderID="HeadContent">
// add your link here
</asp:Content>
just drag your style sheet to your master page.aspx and it will work for all your other forms
You have 3 possible solutions:
Add your link to the head tag of the master page's markup. If you do so, all the pages using the given master page will automatically use your css file.
Use a ContentPlaceHolder in the head tag of your master page. You can use the ContentPlaceHolder at your pages referencing the given master page and you can add your link tag inside the ContentPlaceHolder tag in the markup.
You can add your link tag using Javascrip/jQuery functions.
Although I went the code behind route below in my Page_Load event (VB), I'll give the correct answer to A.K as it seemed to answer the question better but just didn't suit my specific case.
Dim link As New HtmlLink()
link.Attributes.Add("href", Page.ResolveClientUrl("../Css/Generic-Form2.css"))
link.Attributes.Add("Type", "text/css")
link.Attributes.Add("rel", "stylesheet")
Page.Header.Controls.Add(link)
Append the link tag to the head using DOM manipulation.
Normally when we are using Master/Content style pages, we apply the css to Master page so every page child of the master page can use the style but I don't want this,I want I wanna apply css to content page directly instead of master page. Where should I put
<link rel="stylesheet" href="css/lightbox.css" type="text/css" media="screen" />
that reference code ?
Thanks in advance by the way.
Normally I put a content placeholder in the head section of the master page. That way any content page can add extra css/js/etc references to the head of the page.
In your master page put the following
<head>
... title, meta tags, js and css links ...
<asp:contentPlaceholder id="head" runat="server" />
</head>
Then in your pages you can include extra elements in the head using this
<asp:content contentplaceholderid="head" runat="server">
<link rel="stylesheet" href="css/lightbox.css" type="text/css" media="screen" />
</asp:content>
Put a content placeholder in the head portion of your master page. Not all content pages will need to place anything in it but this particular content page can place the CSS file you want to link.
You can't apply a style sheet to a content page only, it applies to the whole web page.
I have a page which has a masterpage. Do I put script and link tags inside the asp:content place holders or outside or does it matter.
When I put it outside, I get the following warning:
Only Content controls are allowed directly in a content page that contains Content controls.
I can see five (or 8) ways of doing it:
In the codebehind (.cs or .vb) using:
Scriptmanager.RegisterClientScriptinclude - using an absolute/relative path
Scriptmanager.RegisterClientScriptInclude - using an embedded resource
Scriptmanager.RegisterSlientScriptBlock - with your source inside
Adding it inline to your ASPX page in the designer
Sticking it inside the asp:content where the content lives inside the body tag.
Sticking it inside the asp:content where the content lives inside the head (You've said this isn't an option, so ignore this).
Adding it programmatically using the ScriptManager inside a control you use on the page as above.
"Only Content controls are allowed directly in a content page that contains Content controls" - did you forget the runat="server"?
If it's scripts and links for all pages, it should go outside any ContentPlaceHolders. If it's scripts and links for this page, it should go inside a Content inside the head. If it's default scripts, put it inside a head ContentPlaceHolder, and it can be replaced by the child page if needed. (VS usually complains about a ContentPlaceHolder in the head, but it works fine for me).
// master Page
<head runat="server">
<asp:ContentPlaceHolder id="head" runat="server">
<!-- Default scripts and CSS -->
<link rel="stylesheet" type="text/css" href="default.css" />
<script type="text/javascript" src="jquery.js"></script>
</asp:ContentPlaceHolder>
<!-- Mandatory scripts and css -->
<link rel="stylesheet" type="text/css" href="all.css" />
<script type="text/javascript" src="all.js"></script>
</head>
<body>
Master Page!
<asp:ContentPlaceHolder id="body" runat="server" />
</body>
// Child (no JQuery)
<asp:Content ContentPlaceHolderID="head" runat="server">
<link rel="stylesheet" type="text/css" href="default.css" />
<!-- Don't need JQuery -->
<script type="text/javascript" src="prototype.js"></script>
</asp:Content>
<asp:Content ContentPlaceHolderID="body" runat="server">
Child Page!
</asp:Content>
// Child 2 (with JQuery)
<asp:Content ContentPlaceHolderID="body" runat="server">
Child Page!
</asp:Content>
If you are referring to <asp:Content /> tags, you can't put anything outside of them in an .aspx page. So you're limited to putting them inside the <asp:Content /> tag. If you want <script /> and <link /> tags you need to either put a <asp:ContentPlaceHolder /> in the <head> of your master page, or add them dynamically via the Page's Controls collection.
Outside. Tthe inside of the ContentPlaceholders is going to be replaced with content from your pages anyhow so it doesn't make much sense to put anything in there.
outside. in the master page
The place holders are the wrapping controls for pages which descend from master pages.
Use asp.net scriptreference tag in master page to add reference to javascript file and you will be able to access all you need in the javascript file as if you would have added to the local page.