|
.net
newsgroups
|
|||||||||||||||||||||||
|
|||||||||||||||||||||||
Master Page prevents using style sheets?I have done a fair amount of style editing inline in ASP. I'm now using VS 2005 with a standard web project (not Web Application Project). This is my first foray into CSS in a style sheet and also my first true attempt at using master pages. I tried setting up a style sheet with a simple setting to float an image to the right and it had no effect on the image. Then, I tried putting the style code in my ASPX file as such, <%@ Page Language="C#" AutoEventWireup="true" MasterPageFile = "~/MyMaster.master" CodeFile="ProductDetails.aspx.cs" Inherits="ProductDetails" %> <style type="text/css"> img { float:right; border:1px dotted black; margin:0px 0px 15px 20px; } </style> <asp:Content ID="Content1" ContentPlaceHolderID="cphMain" Runat="Server"> <asp:Panel ID="pnlProduct" runat="server" Height="50px" Style="z-index: 100; position: relative;" > <asp:Label ID="lblManufacturer" runat="server" Font-Bold="True" Font-Underline="True" Style="z-index: 101; position: relative;" Text="Manufacturer" Font-Size="X-Large"></asp:Label> <br /> <asp:Label ID="lblProductName" runat="server" Font-Bold="True" Font-Underline="True" Style="z-index: 100; position: relative;" Text="Product Name"></asp:Label> <br /> </asp:Panel> <asp:Panel ID="pnlDescription" runat="server" Height="50px" Style="z-index: 102; position: relative; text-align: left" Width="873px"> <asp:Literal ID="ltrDescription" runat="server"></asp:Literal> </asp:Panel> </asp:Content> It complained that "Content is not supported outside 'script' or 'asp:content' regions". I tried moving the style setting inside the asp:content region and it complained that "Element 'style' cannot be nested within element 'td'". This is because my ContentPlaceHolder is inside a table in my master page. As of yet, I can't get the desired layout without doing that. I created a new page, ProductDetails2.aspx, as a stand-alone Web Form page, inserted the style element into the head element and now it works, but I don't have the menu and other standard page formatting that was contained in the master page. So my questions are: 1. What do I need to do to make my style settings work with pages that reference my master page? Do I need to take away all formatting that would require the style element to be nested? 2. Do I need to reference my stylesheet like you would reference a DLL? 3. Does including a stylesheet in the solution/project automatically apply it to all pages in the project? Thanks for your help. JT,
Your styles should appear in the master page. By using master pages the individual "sub pages" do not have direct access to the <HTML><HEAD><BODY> tags (by default). You can add a public property/method to the master page that provides access to the head element though. More or less in the master's .cs file add: public void AddStyles(string styles) { Literal1.Text = styles; } In the master's markup add a literal control in the the <HEAD></HEAD> area. In the sub page's markup add the following declaration (forces Page.Master to be strongly typed) <%@ MasterType VirtualPath="~/masters/SourcePage.master" %> And in the sub page's onload you can do something like: protected void Page_Load(object sender, EventArgs e) { Page.Master.AddStyles("<style>img {float:right;border:1px;} </style>"); } Regards, Rob Show quoteHide quote "JT" <j*@onemain.com> wrote in message news:1157488776.672716.311260@e3g2000cwe.googlegroups.com... > Hi, > > I have done a fair amount of style editing inline in ASP. I'm now > using VS 2005 with a standard web project (not Web Application > Project). This is my first foray into CSS in a style sheet and also my > first true attempt at using master pages. I tried setting up a style > sheet with a simple setting to float an image to the right and it had > no effect on the image. > > Then, I tried putting the style code in my ASPX file as such, > > <%@ Page Language="C#" AutoEventWireup="true" MasterPageFile = > "~/MyMaster.master" CodeFile="ProductDetails.aspx.cs" > Inherits="ProductDetails" %> > <style type="text/css"> > img > { > float:right; > border:1px dotted black; > margin:0px 0px 15px 20px; > } > </style> > > <asp:Content ID="Content1" ContentPlaceHolderID="cphMain" > Runat="Server"> > <asp:Panel ID="pnlProduct" runat="server" Height="50px" > Style="z-index: 100; > position: relative;" > > <asp:Label ID="lblManufacturer" runat="server" Font-Bold="True" > Font-Underline="True" > Style="z-index: 101; position: relative;" > Text="Manufacturer" Font-Size="X-Large"></asp:Label> > <br /> > <asp:Label ID="lblProductName" runat="server" Font-Bold="True" > Font-Underline="True" > Style="z-index: 100; position: relative;" Text="Product > Name"></asp:Label> > <br /> > </asp:Panel> > <asp:Panel ID="pnlDescription" runat="server" Height="50px" > Style="z-index: 102; > position: relative; text-align: left" Width="873px"> > <asp:Literal ID="ltrDescription" runat="server"></asp:Literal> > </asp:Panel> > </asp:Content> > > It complained that "Content is not supported outside 'script' or > 'asp:content' regions". > > I tried moving the style setting inside the asp:content region and it > complained that "Element 'style' cannot be nested within element 'td'". > This is because my ContentPlaceHolder is inside a table in my master > page. As of yet, I can't get the desired layout without doing that. > > I created a new page, ProductDetails2.aspx, as a stand-alone Web Form > page, inserted the style element into the head element and now it > works, but I don't have the menu and other standard page formatting > that was contained in the master page. So my questions are: > > 1. What do I need to do to make my style settings work with pages that > reference my master page? Do I need to take away all formatting that > would require the style element to be nested? > > 2. Do I need to reference my stylesheet like you would reference a DLL? > > 3. Does including a stylesheet in the solution/project automatically > apply it to all pages in the project? > > Thanks for your help. > Sweet! I'll have to explore that.
I just found something in an old HTML book of mine concerning css. I was making a newbie error. I needed to put the following into my master page's <head></head> section: <head runat="server"> <style type = "text/css"> <!-- @import url(MyStyleSheet.css); --> </style> </head> That got my styles going for the site. But I'm thinking I'll have to use your suggestion to override that in specific pages, correct? Thanks for the quick response! I'm sure my eyes will be opened once I realize the power of a css file. No more repeated inline code or include files. I still prefer WinForms apps. JT Rob wrote: Show quoteHide quote > JT, > > Your styles should appear in the master page. By using master pages the > individual "sub pages" do not have direct access to the <HTML><HEAD><BODY> > tags (by default). > > You can add a public property/method to the master page that provides access > to the head element though. More or less in the master's .cs file add: > > public void AddStyles(string styles) > { > Literal1.Text = styles; > } > > In the master's markup add a literal control in the the <HEAD></HEAD> area. > > In the sub page's markup add the following declaration (forces Page.Master > to be strongly typed) > <%@ MasterType VirtualPath="~/masters/SourcePage.master" %> > And in the sub page's onload you can do something like: > > protected void Page_Load(object sender, EventArgs e) > { > Page.Master.AddStyles("<style>img {float:right;border:1px;} </style>"); > } > > Regards, > > Rob > > > > "JT" <j*@onemain.com> wrote in message > news:1157488776.672716.311260@e3g2000cwe.googlegroups.com... > > Hi, > > > > I have done a fair amount of style editing inline in ASP. I'm now > > using VS 2005 with a standard web project (not Web Application > > Project). This is my first foray into CSS in a style sheet and also my > > first true attempt at using master pages. I tried setting up a style > > sheet with a simple setting to float an image to the right and it had > > no effect on the image. > > > > Then, I tried putting the style code in my ASPX file as such, > > > > <%@ Page Language="C#" AutoEventWireup="true" MasterPageFile = > > "~/MyMaster.master" CodeFile="ProductDetails.aspx.cs" > > Inherits="ProductDetails" %> > > <style type="text/css"> > > img > > { > > float:right; > > border:1px dotted black; > > margin:0px 0px 15px 20px; > > } > > </style> > > > > <asp:Content ID="Content1" ContentPlaceHolderID="cphMain" > > Runat="Server"> > > <asp:Panel ID="pnlProduct" runat="server" Height="50px" > > Style="z-index: 100; > > position: relative;" > > > <asp:Label ID="lblManufacturer" runat="server" Font-Bold="True" > > Font-Underline="True" > > Style="z-index: 101; position: relative;" > > Text="Manufacturer" Font-Size="X-Large"></asp:Label> > > <br /> > > <asp:Label ID="lblProductName" runat="server" Font-Bold="True" > > Font-Underline="True" > > Style="z-index: 100; position: relative;" Text="Product > > Name"></asp:Label> > > <br /> > > </asp:Panel> > > <asp:Panel ID="pnlDescription" runat="server" Height="50px" > > Style="z-index: 102; > > position: relative; text-align: left" Width="873px"> > > <asp:Literal ID="ltrDescription" runat="server"></asp:Literal> > > </asp:Panel> > > </asp:Content> > > > > It complained that "Content is not supported outside 'script' or > > 'asp:content' regions". > > > > I tried moving the style setting inside the asp:content region and it > > complained that "Element 'style' cannot be nested within element 'td'". > > This is because my ContentPlaceHolder is inside a table in my master > > page. As of yet, I can't get the desired layout without doing that. > > > > I created a new page, ProductDetails2.aspx, as a stand-alone Web Form > > page, inserted the style element into the head element and now it > > works, but I don't have the menu and other standard page formatting > > that was contained in the master page. So my questions are: > > > > 1. What do I need to do to make my style settings work with pages that > > reference my master page? Do I need to take away all formatting that > > would require the style element to be nested? > > > > 2. Do I need to reference my stylesheet like you would reference a DLL? > > > > 3. Does including a stylesheet in the solution/project automatically > > apply it to all pages in the project? > > > > Thanks for your help. > > "Rob" <rmacfadyen_at_gmail.com> wrote in message Yes they do - just add runat="server" to the tag e.g.news:Ob0UV0T0GHA.1292@TK2MSFTNGP03.phx.gbl... > By using master pages the individual "sub pages" do not have direct access > to the <HTML><HEAD><BODY> tags (by default). <header runat="server"> .... .... .... </head> Then, in your codefile, you can do something like: Style objStyle = new Style(); objStyle.ForeColor = System.Drawing.Color.Yellow; Header.StyleSheet.CreateStyleRule(objStyle, null, "td"); Furthermore, to help clarify Rob's misunderstanding there are new classes
unique to the head. Start with the following search term and look through the other related classes listed in the tree that is displayed with MSDN documentation. htmlhead class site:msdn2.microsoft.com <%= Clinton Gallagher NET csgallagher AT metromilwaukee.com URL http://clintongallagher.metromilwaukee.com/ MAP 43°2'17"N 88°2'37"W : 43°2'17"N 88°2'37"W Show quoteHide quote "Mark Rae" <mark@markNOSPAMrae.com> wrote in message news:uZxRr$T0GHA.1256@TK2MSFTNGP04.phx.gbl... > "Rob" <rmacfadyen_at_gmail.com> wrote in message > news:Ob0UV0T0GHA.1292@TK2MSFTNGP03.phx.gbl... > >> By using master pages the individual "sub pages" do not have direct >> access to the <HTML><HEAD><BODY> tags (by default). > > Yes they do - just add runat="server" to the tag e.g. > > <header runat="server"> > ... > ... > ... > </head> > > Then, in your codefile, you can do something like: > > Style objStyle = new Style(); > objStyle.ForeColor = System.Drawing.Color.Yellow; > Header.StyleSheet.CreateStyleRule(objStyle, null, "td"); > > Whoopsie :)
I am of course massively incorrect. As pointed out: this.Header.StyleSheet.CreateStyleRule(...) is exactly what you're looking for. I guess I was just to impressed with strongly typed master pages. Thanx to Mark Rae and Clinton for keeping things straight :) Regards, Rob MacFadyen I've eaten more crow than you'll ever know ;-)
<%= Clinton Gallagher Show quoteHide quote "Rob MacFadyen" <rmacfadyen_at_gmail.com> wrote in message news:uuOUgbc0GHA.3568@TK2MSFTNGP03.phx.gbl... > Whoopsie :) > > I am of course massively incorrect. As pointed out: > > this.Header.StyleSheet.CreateStyleRule(...) > > is exactly what you're looking for. > > I guess I was just to impressed with strongly typed master pages. > > Thanx to Mark Rae and Clinton for keeping things straight :) > > Regards, > > Rob MacFadyen > "clintonG" <csgallag***@REMOVETHISTEXTmetromilwaukee.com> wrote in message Likewise... :-)news:e7$867d0GHA.3476@TK2MSFTNGP04.phx.gbl... > I've eaten more crow than you'll ever know ;-) I like mine with peppercorn sauce. Mmmm... mmmm!
Thanks again for the help! Mark Rae wrote: Show quoteHide quote > "clintonG" <csgallag***@REMOVETHISTEXTmetromilwaukee.com> wrote in message > news:e7$867d0GHA.3476@TK2MSFTNGP04.phx.gbl... > > > I've eaten more crow than you'll ever know ;-) > > Likewise... :-)
Other interesting topics
Problem with RaiseCallbackEvent
wildcard mapping Delayed Code Execution? Authentification - Server Variables ( omg! ) I/O stream performance Security exception with impersonate true for a webservice Help debugging...where do I look? making sure page loads Overture/Yahoo & Google PPC referral tracking Is it possible to have more than one login control in a website? |
|||||||||||||||||||||||