I have a master page file and I have always added additional style sheets pragmatically using Page.Header.Controls.Add etc. But we have a client that wants the css files in a specific place, so I noticed that in the head section there is a
<asp:ContentPlaceHolder ID="HeadContent" runat="server">
so I thought I could just add it into there.
I have tried
ContentPlaceHolder blar = (ContentPlaceHolder)Master.FindControl("HeadContent");
blar.Controls.Add(css);
But nothing appears, can anyone help?
U can add your css file like that;
ContentPlaceHolder blar = (ContentPlaceHolder)Master.FindControl("HeadContent");
HtmlLink css= new HtmlLink();
css.Attributes.Add("rel", "stylesheet");
css.Attributes.Add("type", "text/css");
css.Href = "/Styles/site.css";
blar.Controls.Add(css);
Check out the answer to this question: https://stackoverflow.com/a/2969360/1372321
It seems to do what you want :)
Update
To add content at a specific place in the header, and you are using .Net 4.5, you can use the System.Web.Optimatization API and bundle the styles. Then in your head, you can use the provided control:
<webopt:BundleReference runat="server" Path="~/Content/css" />
You can see an example of this in the default WebForms project template.
If you're using an older version of .Net, you can use content placeholder in your master page, and then add controls to that specific placeholder like so:
In master page head
<asp:ContentPlaceHolder runat="server" ID="HeadContent" />
In aspx page
<asp:Content runat="server" ContentPlaceHolderID="HeadContent">
<asp:PlaceHolder runat="server" ID="placeHolderHead"></asp:PlaceHolder>
</asp:Content>
Then you can simply add the Link controls to the placeholder programatically.
Related
I have got a master page with three content holders. Say; Header, Menu and Content.
I can use Iframes or regular frames, but is there a way to specify the three content holders as follows and redirect them to three different web forms?
The reason I ask this, is because I do not want to specify my menu over and over again for every single web form.
<asp:Content ID="Content1" ContentPlaceHolderID="Header" runat="server">
</asp:Content>
<asp:Content ID="Content2" ContentPlaceHolderID="Menu" runat="server">
</asp:Content>
<asp:Content ID="Content3" ContentPlaceHolderID="Content" runat="server">
</asp:Content>
You've missed the point of a master page. A master page should contain the markup/code that is common to the content pages or nested master pages that reference it. Put the menu code into the master page.
<!-- Menu code here -->
<ul class="menu">
<li>Home"</li>
<li>Contact"</li>
</ul>
!-- End menu code -->
<asp:ContentPlaceHolder ID="MenuPlaceHolder" runat="server">
<!-- On the content page, your page specific menu code would go in the <asp:Content> that references this -->
</asp:ContentPlaceHolder >
As an alternative to (or in conjunction with) master pages, you can use user controls. You place your menu code in the user control and embed it in master pages or content pages. But if you always want it in the same place, then a master page makes more sense because of the Don't Repeat Yourself principle.
There is absolutely no reason to use an iframe for this.
My ASP.NET page was not showing the controls. It just shows that master page error
The page has one or more <asp:content> controls that do not correspond
with <asp:ContentPlaceHolder controls in the Master Page
Try setting
<title></title>
instead of
<title />
Remove comment (<%-- --%>) in the Master file, this worked for me.
All your Content controls have to reference existing ContentPlaceHolders on master page
For instance, if you have on your page
<asp:Content ContentPlaceHolderID="Header" ID="Title" runat="server">
You must have something like this on your master page
<asp:ContentPlaceHolder ID="Header" runat="server" />
ContentPlaceHolderID is the property that must match against any ContentPlaceHolder ID on master page.
The problem is that you must have a syntax error in the master.page's markup :
Ex) %²> in stead of %>
If you validate your markup, I'm sure thar this will solve your problem
PI. I has the same error & it works for me ;)
I have a Master Page for all my ASP.NET pages. It works fine for 95% of pages... Except that it also includes unwanted attributes that are included in the other 5% of pages. This causes application problems. Is it possible for Content Pages to somehow select to enable/disable a feature available in the master page? One option I can think of is for the Master Page to look at the page name and then decide what to do, but that's a bit clunky in the long run...
You can add a MasterType directive in your content page so you will have access to the master page class... and from there you can implement to enable disable features of your master page...
<%# MasterType virtualpath="~/MyMaster.master"%>
and in your code behind you will have access to the Master property as a typed class...
In your master page provide a content placeholder:
<asp:ContentPlaceHolder ID="foo" runat="server">
<div>Some default content</div>
</asp:ContentPlaceHolder>
In the 5% of the pages that don't need the default behavior override this placeholder with an empty string:
<asp:Content ID="Content1" runat="server" ContentPlaceHolderID="foo" />
The other 95% of the pages will get the common behavior.
You can access the master page directly by getting at the page and casting it to the type of your master page as below. This will couple you to the master page but the only other option I can think of is to create a key in session.
(MyMasterPage)Page.Master
Assuming that these features are represented in the asp markup of the master page, you could wrap it within a ContentPlaceHolderControl:
<asp:ContentPlaceHolder ID="OptionalContent" runat="server">
*** This is my optional content ***
</asp:ContentPlaceHolder>
In the 95% of pages where this content works, you can just not include a Content control for "OptionalContent" on your page. For the 5% where it doesn't work, you can include an empty Content control for "OptionalContent":
<asp:Content ContentPlaceHolderID="OptionalContent" runat="server"></asp:Content>
How can I apply CSS to a Web Content Form that is using a MasterPage?
I can't apply Class="" anywhere. :S Any help?
in Web Controls the attribute is CssClass and not Class as it is in HTML Controls
<style type="css/text">
.myClass { font-size: x-large; }
</style>
then
<asp:Label ID="myLabel" runat="server"
CssClass="myClass" Text="This is a WebControl Label" />
as opposed to:
<span class="myClass">This is a HTML Lable</span>
Why can't you?
CSS works in content pages the smae way it works in regular pages.
If you're actually asking how to add CSS rules for the content page, you need to add a placeholder in the HEAD element in the master page, then add CSS rules or files to that placeholder in the content page.
I've never had an issue with this. If it's not rendering, then make sure your path to your CSS file is correct.
Be sure you stylesheet is referenced properly in your web content form.
<link id="link1" rel="stylesheet" href="myStyleSheet.css" type="text/css" runat="server" />
I have created a custom master page and have the follwoing code on the page:
<asp:ContentPlaceHolder id="PlaceHolderSearchArea" runat="server">
<SPSWC:SearchBoxEx id="SearchBox"
RegisterStyles="false"
TextBeforeDropDown=""
TextBeforeTextBox="Search"
TextBoxWidth="120"
GoImageUrl="/_layouts/images/gosearch.png"
GoImageUrlRTL="/_layouts/images/gosearch.png"
UseSiteDefaults="true"
DropDownMode = "HideScopeDD"
SuppressWebPartChrome="true"
runat="server"
WebPart="true"
__WebPartId="{07E563F9-A259-4829-920F-03829BBC14D1}"
GoImageActiveUrl="/_layouts/images/gosearch.png"
GoImageActiveUrlRTL="/_layouts/images/gosearch.png"/>
</asp:ContentPlaceHolder>
On one site this code works correctly and on another it does not work and the default search box apears. I can not work out why this is happening. Any ideas?
The content in the contentplaceholder "PlaceHolderSearchArea" can be overwritten by a <asp:Content /> element in a page layout. So check the page layouts you are using for the existance of an <asp:Content/> with the placeholder id "PlaceHolderSearchArea".