By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
446,267 Members | 1,861 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 446,267 IT Pros & Developers. It's quick & easy.

Loading CSS & JS Files using Master Pages (ASP.NET C# Web app)

P: 32
Hi,

I have a master page & various pages that will use this master page. Each content page will have a CSS & JS file which will be named the same as the content page.

When I try to load the CSS & JS files using the code behind of the content page, my buttons aren't rendered and Im not sure why?

Here is the code for my content page - Properties.aspx:-

Expand|Select|Wrap|Line Numbers
  1. <%@ Page Language="C#" AutoEventWireup="true" Title="Properties" CodeFile="Properties.aspx.cs" Inherits="Properties" MasterPageFile="~/MasterPage.master"%>
  2.  
  3. <asp:Content ID="_propertiesContent" ContentPlaceHolderID="_mainContentHolder" runat="Server">
  4.     <asp:Label ID="_test" Text="This is the main content panel holder" runat="server" />
  5.     <br />
  6. </asp:Content>
  7.  
  8. <asp:Content ID="_buttonContent" ContentPlaceHolderID="_buttonContentHolder" runat="server" >
  9.     <asp:Button ID="Button1" runat="server" Text="Save" CssClass="button" />
  10.     <asp:Button ID="Button2" runat="server" Text="Undo" CssClass="button" />
  11.     <br />
  12. </asp:Content>
  13.  
  14. <asp:Content ID="_validationContent" ContentPlaceHolderID="_validationContentHolder" runat="server">
  15.     <asp:ValidationSummary ID="validationSummary" ValidationGroup="ValidationGroup" runat="Server" />
  16. </asp:Content>
Here is the code behind for Properties.aspx.cs:-

Expand|Select|Wrap|Line Numbers
  1. using ASP;
  2. using System;
  3. using System.Data;
  4. using System.Configuration;
  5. using System.Collections;
  6. using System.Web;
  7. using System.Web.Security;
  8. using System.Web.UI;
  9. using System.Web.UI.WebControls;
  10. using System.Web.UI.WebControls.WebParts;
  11. using System.Web.UI.HtmlControls;
  12.  
  13. public partial class Properties : Page
  14. {
  15.     private string _pageName;
  16.  
  17.     protected void Page_Load(object sender, EventArgs e)
  18.     {
  19.         _pageName = Page.GetType().Name.Substring(0, Page.GetType().Name.IndexOf("_"));
  20.  
  21.         if (!IsPostBack)
  22.         {
  23.             LoadStyleSheets();
  24.             LoadJavascriptFile();
  25.         }
  26.     }
  27.  
  28.     private void LoadStyleSheets()
  29.     {
  30.         HtmlLink link = new HtmlLink();
  31.         link.Href = string.Format("~/{0}.css", _pageName);
  32.         link.Attributes["text"] = "text/css";
  33.         link.Attributes["rel"] = "stylesheet";
  34.         this.Header.Controls.Add(link);
  35.     } 
  36.  
  37.     private void LoadJavascriptFile()
  38.     {
  39.          HtmlGenericControl javaScriptLink = new HtmlGenericControl();
  40.         javaScriptLink.TagName = "script";
  41.         javaScriptLink.Attributes.Add("type", "javascript");
  42.         javaScriptLink.Attributes.Add("src", string.Format("JS/{0}.js", _pageName));
  43.     }
  44. }

My Master Page code is:-

Expand|Select|Wrap|Line Numbers
  1. <%@ Master Language="C#" AutoEventWireup="true" CodeFile="MasterPage.master.cs" Inherits="MasterPage" Debug="true"%>
  2.  
  3. <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
  4.  
  5. <html xmlns="http://www.w3.org/1999/xhtml" >
  6. <head id="Head1" runat="server">
  7.     <title>Master Page</title>
  8. </head>
  9.  
  10.  
  11. <body>
  12.     <form id="MainForm" runat="server">        
  13.  
  14.         <!-- This bit in here needs to be configurable per page, main content section -->
  15.         <asp:ContentPlaceHolder ID="_mainContentHolder" runat="server" />
  16.  
  17.         <!-- This bit in here needs to be configurable per page, button section -->
  18.         <asp:ContentPlaceHolder ID="_buttonContentHolder" runat="server" />
  19.  
  20.         <!-- This bit is for the validation summary for each page -->
  21.         <asp:ContentPlaceHolder id="_validationContentHolder" runat="server" />
  22.  
  23.  
  24.     </form>
  25. </body>
  26.  
  27. </html>

If I comment out the methods to Load CSS file & Load JS files in my properties page - then my label text & my buttons are rendered, but if I leave these methods in, then only my Label text renders - Any ideas why?

Many thanks
Janet
Jul 12 '07 #1
Share this question for a faster answer!
Share on Google+

Post your reply

Sign in to post your reply or Sign up for a free account.