473,509 Members | 2,526 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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

32 New Member
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
0 2734

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

Similar topics

4
2503
by: Rob Meade | last post by:
Hi all, I have just put together our organisations 'template' for our web applications and have created 7 .ascx files which when dropped into my template file work perfectly...however, I have a...
1
2910
by: Brian | last post by:
I've looked through the previous posts on this one and have verified permissions and location of my Dlls, but I am still getting an exception when I try to Load an assembly. The directory where...
4
1446
by: Paul | last post by:
Hi still testing the idea of using html files for forms and then integrating this into a net app. I tried a small form with a button but noticed all I could do with the button is to right click and...
20
2396
by: Alan Silver | last post by:
Hello, In classic ASP, I used to use two include files on each page, one before and one after the main content, to provide a consistent layout across a web site. That way I could just change the...
1
1796
by: Fred Nelson | last post by:
Hi: I'm working on one of my first web applications in asp.net 2.0 and I'm having a problem with absolute versus relative positioning of controls that I place on pages that use master pages with...
2
3729
by: Rob R. Ainscough | last post by:
I'm using forms authentication along with Master pages -- my authenticated forms reside is a sub-dir (Secure) below the web site root dir. The problem is that any graphics placed on the Master...
17
2822
by: Arpan | last post by:
When a Button is clicked in a Web Form in an ASPX page, the Form will post back to itself. Under such circumstances (i.e. when a Button is clicked), will the Page_Load sub execute first & then will...
6
3489
by: Homer J. Simpson | last post by:
Hi all, I have enough experience with HTML/classic ASP to get by, and I'm trying to learn ASP.NET. Traditionally, I've taken the habit of breaking out extra-long CSS files into multiple,...
7
1954
nukefusion
by: nukefusion | last post by:
Hi all, I'm trying to think of the best way to achieve a certain goal and thought I would draw on the experience here. What I am currently doing is compiling a set of HTML files that form the...
0
7233
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
7135
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
1
7067
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
7505
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
5650
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
4729
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
3201
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
1570
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
1
774
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.