Having to deal with URL rewriting, I decided to programmatically add CSS references rather than relying on the automatic behavior of ASP.NET to add all CSS files within a theme automatically to the <head> of an ASPX page.
I suceeded in adding the CSS file programmatically by using
<link type="text/css" rel="stylesheet" runat="server" id="CssFile" />
and setting the actual URL it in code behind.
My question is:
Is there a way to prevent the ASP.NET "engine" from automatically adding all CSS files into the <head> of my ASPX/master page?
(Of course, I still could go with my own folders and completely drop the App_Themes concept)
You can prevent this feature by adding the following attributes to the Page directive:
<%# Page ... EnableTheming="false" Theme="" StylesheetTheme="" %>
I do not believe you can tell a skin to not include the files within it, that was the killer for us when it came to using them as it referenced all the css files not just the ones we needed and we needed maximum efficiency.
<head runat="server" visible="false" />
<%# Master Language="C#" AutoEventWireup="true" Inherits="Front" Codebehind="Front.master.cs" **EnableTheming="false"** %>
was my solution to getting around the problem.
Related
Every page in my ASP.NET application is based on Main masterpage, which in its turn is based on another Base masterpage (nested masterpages). I try to set different title for every page. I tried two approaches, but it does not work without any obvious reason.
1st attempt: <%# Page Title="ABC" ....... %>. I do not know why, but it does nothing. No <title> tag is generated in resulting HTML.
2nd attempt: <%# Page meta:resourcekey="PageTitle" ....... %> and PageTitle.Title to .resx. Same here. No <title> tag at all.
What did I miss?
I discovered the reason: looong time ago I removed runat="server" from my <head> tag. When I add it back - the Title directive starts to work, but it breaks other functionality, like <link rel="stylesheet" type="text/css" href="/css/<%= www.Utils.LanguageName %>/language14.css" />.
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.
I have the following situation on my new ASP.Net page:
I am using a master page
I am using themes
I have pages in separate folders
I need to reference a favicon from my master page based on the current theme.
Unfortunately the ~App_Themes/Basic/Images/favicon.ico path resolves to http://example.com/folder/App_Themes/Basic/Images/favicon.ico.
How can I uniformly refer to my favicon.ico located in the App_Themes/Basic/Images/favicon.ico path from master page used by the following differently located pages:
~/Home.aspx
~/Secure/Dashboard.aspx
~/Accounts/Login.aspx
Usually ASP.NET themes are limited to skin files and CSS files with all images referenced from the CSS file. In that scenario, the paths to images are relative from the CSS file.
If you need a path to a file inside the current theme's folder relative from a page, you can use the Page.Theme property combined with the Page.ResolveUrl() method:
<%= Page.ResolveUrl(String.Format("~/App_Themes/{0}/Images/favicon.ico", Page.Theme)) %>
If you want to use that in a <link rel="shortcut icon"> element you can just put the code above inside the href attribute. Unless you have a <head runat="server">, in which case ASP.NET may throw an HttpException:
The Controls collection cannot be modified because the control
contains code blocks (i.e. <% ... %>).
This can be fixed by putting the <link> element inside an <asp:PlaceHolder> control:
<head runat="server">
<asp:PlaceHolder runat="server">
<link rel="shortcut icon" href="<%= ... %>" />
</asp:PlaceHolder>
</head>
In my current project we have 5 different masterpages, there are some common elements in each and its really annoying making the change in all 5, it kind of defeats the point of masterpages.
I have tried having parent and child master pages but that caused other problems for a different day.
Is there a way to include dynamic content in a masterpage?
I'm looking for something similar to the php and coldfusion include().
You can put user controls (.ASCX) in your master pages. Is this what you were attempting to accomplish?
Like so...
<%# Master Language="C#" AutoEventWireup="true" CodeBehind="WebForms.master.cs"
Inherits="Tunafish.Web.Views.Shared.WebForms" %>
<%# Register Src="~/Content/Controls/SiteNavigation.ascx" TagName="Nav"
TagPrefix="sc" %>
<html xmlns="http://www.w3.org/1999/xhtml">
<head id="Head1" runat="server">
<asp:ContentPlaceHolder ID="HeadContent" runat="server" />
</head>
<body>
<sc:Nav runat="server" />
Have you looked into User Controls? .ascx
Custom web controls are the way to go:
http://weblogs.asp.net/scottgu/archive/2006/11/26/tip-trick-how-to-register-user-controls-and-custom-controls-in-web-config.aspx
As mxmissile and JMP suggested, user controls are the way to go, but you might want to be thorough in your usage of them. When you include the master page, make sure you add the following markup along with the page declaration:
<%# MasterType
virtualpath="~/myMasterPages/Master.master"
%>
This will allow you to call functions/objects in your master pages so you can make changes to controls or have access from the page itself to various other objects. I have a property in my base usercontrol class called "ParentForm" that is a reference to the page it sits in. For user controls in the master page, I ended up having the same property and in the setter of that property I translate it down to the user controls.
You could set the masterpages to inherit from a class that dynamically inserts content or script to the page OnPreRender in code. It may seem out there but I have had to use this method.
I've got a couple of ASP.Net Usercontrols that I am using in different locations of my new website. These usercontrols had links like this :
If the usercontrol is used in pages in various subdirectories the relative path just doesn't work, and I don't want to provide my full website name in the path. So I did this
<a href="~/daily/panchang/" runat="server">
and now the ASP.Net '~' marker works correctly to resolve the root path.
Is it okay to mark all my HTML tags where I have the need to resolve the root path with runat="server" or do you know of a better, HTML way?
Thanks
I won't say whether it's an elegant solution, I'll just point out an alterantive within System.Web:
<a href="<%= VirtualPathUtility.ToAbsolute("~/daily/panchang/") %>">
You should use a base tag to define the root of your application and make all links relative like this :
<head>
<base href="<%= Request.ApplicationPath %>" />
</head>
...
<!-- this now points to ~/daily/panchang/ -->
Be careful though because every element that has runat="server" will be 'serialized' and stored in the ViewState every time a PostBack occurs, and you don't wanna be cluttering it up with useless data.