Include CSS file in view - asp.net

In my view, I have included the Master page as follows:
<%# Page Title=""
Language="C#" MasterPageFile="~/Views/Shared/Mall.Master"
Inherits="System.Web.Mvc.ViewPage<DomainModel.Entities.Seller>" %>
And in my Mall.master file, I add a link to include a general css file
<link rel="Stylesheet" href="../../Content/MallMaster.css" type="text/css" />
However, I need another more specific purpose css file CheckOut.css in my view. Since the style defined in Checkout.css only applies for one page, I do not want to include the file in the master page. Is there any way to include the file in my view?

You should add a new content () placeholder in your MasterPage and then in your view you can add another css to this placeholder.
<asp:ContentPlaceHolder ID="head" runat="server">
<title></title>
<link rel="Stylesheet" href="../../Content/MallMaster.css" type="text/css" />
</asp:ContentPlaceHolder>
This may be helpful for you - How to pass page’s meta tags in ASP.NET MVC?

If you don't want to override existing tags in the master, you can add a content placeholder inside the head tag too:
<head runat="server">
<title>
<asp:ContentPlaceHolder ID="title" runat="server">
Default Title</asp:ContentPlaceHolder>
</title>
<!-- The ContentPlaceHolder is placed inside the title tag to make sure that the
document validates in the VS editor - <title> needs to be a direct child of
<head>. The output will validate. -->
<script src="theAllFamous_jQuery.js" type="text/javascript" />
<link href="sitewide.css" type="text/css" rel="Stylesheet" />
<asp:ContentPlaceHolder ID="headContent" runat="server />
</head>
And in your view:
<asp:Content ID="title" ContendPlaceHolderId="title" runat="server">
Page specific title
</asp:Content>
<asp:Content ID="head" ContentPlaceHolderId="headContent" runat="server">
<link href="pagespecific.css" type="text/css/ rel="Stylesheet" />
</asp:Content>
That way you won't have to duplicate code that you want in all head tags on your site.

Related

Umbraco inline razor #section in head tag

I'm trying to create razor #section in one of my views, using umbraco:Macro tag, in order to add scripts/styles specific for each view into head tag.
My current code looks like this(head section of master page):
<asp:Content ContentPlaceHolderID="ContentPlaceHolderDefault" runat="server">
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8" />
<meta name="viewport" content="width=device-width" />
<link rel="stylesheet" type="text/css" href="/css/foundation.min.css">
<script type="text/javascript" src="/js/jquery-1.10.1.min.js"></script>
<script type="text/javascript" src="/js/responsiveslides.js"></script>
<script type="text/javascript" src="/js/js.js"></script>
<umbraco:Macro runat="server" language="cshtml">
#if(IsSectionDefined("JSIncludes"))
{
#RenderSection("JSIncludes")
}
#if(IsSectionDefined("CSSIncludes"))
{
#RenderSection("CSSIncludes")
}
</umbraco:Macro>
</head>
and this(actual section in view):
<asp:Content ContentPlaceHolderId="ContentPlaceHolderDefault" runat="server">
<umbraco:Macro runat="server" language="cshtml">
#section JSIncludes
{
<script type="text/javascript" src="/js/main_page.js"></script>
}
</umbraco:Macro>
But when i'm trying to open a page, i'm getting "Error loading MacroEngine script (file: )" error on top of my page.
Do anyone know the reason of this?
Would be great, if somebody knew how to add things like this properly.
Thanks in advance.
You don't need to use a razor macro to add page-specific script references, use a ContentPlaceHolder
Master page:
<head>
<asp:ContentPlaceHolder ID="PageScripts" runat="server" />
<asp:ContentPlaceHolder ID="PageStyles" runat="server" />
Content page:
<asp:Content ID="PageScript" runat="server" ContentPlaceHolderID="PageScripts">
<script type="text/javascript" src="/js/main_page.js"></script>
</asp:Content>
Reference

page document mode

I've an aspx page that extends a master page. I want to change aspx page's document mode. I'd like to put this line to aspx page. But It doesn't allow. I don't want to put this code to master page's head want only change the page's document mode. Can somebody help me?
<meta http-equiv="X-UA-Compatible" content="IE=9" />
You need a placeholder in your masterpage:
<head>
<asp:ContentPlaceHolder id="plhHead" runat="server"/>
</head>
If your <html/> tag has no runat="server", you need to apply it to the <head/> tag like KPL did.
And then fill it in the client page like you do with your main content placeholder:
<asp:Content ContentPlaceHolderId="plhHead" runat="server">
<meta http-equiv="X-UA-Compatible" content="IE=9" />
</asp:Content>
Place a ContentPlaceHolder in the head section of your Master Page:
<head runat="server">
<asp:ContentPlaceHolder ID="HeadContent" runat="server">
</asp:ContentPlaceHolder>
</head>
Now from your .aspx page, you can add custom content in the head section:
<asp:Content ID="HeaderContent" runat="server" ContentPlaceHolderID="HeadContent">
<meta http-equiv="X-UA-Compatible" content="IE=9" />
</asp:Content>
As an alternative to placing a ContentPlaceHolder in the Master page, you can do this:
// Programmatically add a <meta> element to the Header
HtmlMeta keywords = new HtmlMeta();
keywords.Name = "X-UA-Compatible";
keywords.Content = "IE=9";
Page.Header.Controls.Add(keywords);

adding css class in content pages

I am working on a page which is using a Master page for some default design,
I need some css class to be added to the conent page, which we add normally in script tag in normal pages,
My question is how to add some css content in a page which is using Master page,
Thanks,
Vishal.
In your master page you can add a content area within the <head> block. We call ours "HeadContent".
Our master page's head block looks like this:
<head>
...
<asp:ContentPlaceHolder ID="HeadContent" runat="server" />
</head>
From your content pages you can then include custom scripts/css etc:
<asp:Content runat="server" ID="Head" ContentPlaceHolderID="HeadContent">
<script type="text/javascript" src="<%= Url.Content("~/scripts/gradebook.js") %>"></script>
<style type="text/css">
#import url('<%= Url.Content("~/styles/gradebook.css") %>');
</style>
</asp:Content>
You could add a ConentPlaceHolder in the head area of your masterpage like this:
<head>
....
<asp:ContentPlaceHolder id="PlaceHolderAdditionalPageHead" runat="server" />
</head>
In your content page you just need a content control:
<asp:Content ID="Content1" ContentPlaceHolderID="PlaceHolderAdditionalPageHead" runat="Server">
// Put your css stuff here
</asp:Content>

ASP.NET 3.5: Master Page stylesheet variablized?

I'm new to ASP.NET coming from a PHP and ColdFusion background and I have a pretty simple question:
Within my Master Page, how can I make one of my CSS file links a variable, so when the appropriate page (Home for example) comes in, it contains the variable with the correct CSS file to use?
<!-- Custom CSS Files -->
<link href="<Page Specific CSS Variable>" rel="stylesheet" type="text/css" />
<link href="../../Content/Styles/Site.css" rel="stylesheet" type="text/css" />
</head>
Would this be the answer? Home.aspx
<asp:Content ID="Content1" ContentPlaceHolderID="PageStylesheet" runat="server">
<link href="../../Content/Styles/Home.css" rel="stylesheet" type="text/css" />
</asp:Content>
And then in Master Page:
<!-- Custom CSS Files -->
<asp:ContentPlaceHolder ID="PageStylesheet" runat="server" />
Is that right?
One solution would be to add a property to the master page that you can set from each web content form. So for example:
MasterPage.Master
<!-- Custom CSS Files -->
<link href="<%=this.PageSpecificCSSURL%>" rel="stylesheet" type="text/css" />
<link href="../../Content/Styles/Site.css" rel="stylesheet" type="text/css" />
</head>
MasterPage.Master.cs
private string mPageSpecificCSSURL = string.Empty;
public string PageSpecificCSSURL
{
get
{
return mPageSpecificCSSURL;
}
set
{
mPageSpecificCSSURL = value;
}
}
WebForm1.aspx.cs
protected void Page_Load(object sender, EventArgs e)
{
((SiteMaster) this.Master).PageSpecificCSSURL = "mypage.css";
}
Use a content section in the page header in the master page. That's its purpose; for CSS and/or script and whatnot that are specific to a content page. The content will be merged with the content of the master page, resulting in a full head section. All content placeholders can be put pretty much anywhere; title, head, foot, whatever you like, and you can nest master pages. They are just templates; you don't have to restrict yourself to BODY content.
<%# Master Language="C#" AutoEventWireup="true" CodeBehind="REO.master.cs" Inherits="REO.REO" %>
<!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>REO CMS</title>
<link rel="Stylesheet" href="REO.css" type="text/css" />
<script src="jquery-1.4.2.min.js" type="text/javascript"></script>
<script src="jqueryui-1.8%20jquery-ui.min.js" type="text/javascript"></script>
<link href="http://ajax.googleapis.com/ajax/libs/jqueryui/1.8/themes/base/jquery-ui.css" rel="stylesheet" type="text/css" />
<asp:ContentPlaceHolder ID="head" runat="server"></asp:ContentPlaceHolder>
</head>
<%# Page Language="C#" AutoEventWireup="true" CodeBehind="AddCaseContact.aspx.cs"
Inherits="REO.AddCaseContact" MasterPageFile="~/REO.Master" %>
<asp:Content ContentPlaceHolderID="head" runat="server">
<script type="text/javascript">
function showMessage(wTitle, msg) {
var $dialog = $('<div></div>')
.html(msg)
.dialog({
autoOpen: false,
title: wTitle,
modal: true,
height: 300,
buttons: { "Ok": function() { $(this).dialog("close"); } }
});
$dialog.dialog('open');
}
</script>
</asp:Content>
<asp:Content ContentPlaceHolderID="MainContent" runat="server">
With you're help, the answer is within my edits. Perfect.

Links css file to a content page

I have Master page and some content pages.
I want to assign different css file to each of the content pages.
(without using themes)
How can I do that?
I did that once by adding a header-placeholder in the master-page, and explicitly specifying the css in the content-pages.
In the Master:
<head runat="server">
<title></title>
<link href="~/css/common.css" rel="stylesheet" type="text/css" />
<!-- loads of other stuff / -->
<asp:ContentPlaceHolder ID="head" runat="server">
</asp:ContentPlaceHolder>
</head>
and in the Content:
<asp:Content ID="Content1" ContentPlaceHolderID="head" runat="server">
<link href="../css/SomeContent.css" rel="stylesheet" type="text/css" />
<script src="../js/SomeJsToo.js" type="text/javascript"></script>
</asp:Content>
If you're using visual studio 2008, you're going to have a real easy time. First make a master page like this:
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></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>
Now make a content page based off of this master page:
<asp:Content ID="Content1" ContentPlaceHolderID="head" Runat="Server">
</asp:Content>
<asp:Content ID="Content2" ContentPlaceHolderID="ContentPlaceHolder1" Runat="Server">
</asp:Content>
Now in the Content1 placeholder you just place the stylesheet that you would like to have applied to that page.
That's it. Hope this works for you.
Use an external master CSS file for all pages using:
<link rel="stylesheet" type="text/css" href="master.css" />
Then you can use embedded CSS on the individual content pages using the style tag, e.g:
<style type="text/css">
h1 {color:red}
p {color:blue}
</style>
I have tried many of the above ways but still getting error. Finally i use the following codes on the page load and it works fine:
Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
Dim css1 As New HtmlLink
css1.Href = ResolveUrl("report.css")
css1.Attributes("rel") = "stylesheet"
css1.Attributes("type") = "text/css"
css1.Attributes("media") = "all"
Me.Page.Header.Controls.Add(css1)
End Sub

Resources