473,651 Members | 2,644 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 2743

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

Similar topics

4
2510
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 question... As our team develops several applications I wanted these generic .ascx's to be able to be used by all - therefore I've placed them in the root of the domain we work on in a directory called /WebUserControls
1
2919
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 my Dlls are located give the ASPNET user full control, and all Dlls used by the Assembly in question are in the local folder with the rest of the web application Dlls. Could someone take a look at my error and shed some light on this?
4
1456
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 select run at server. This still does not seem the same as using a button from the .net tool box. Just wondering why, thanks. -- Paul G Software engineer.
20
2417
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 include files to change the layout. When I came to ASP.NET, I used user controls to do a similar thing. I have just been looking at master pages, and it looks like they do the same thing. If so, is there any advantage in using them over the...
1
1802
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 Content Place Holders. The controls placed in a content area with absolute or relative positioning can easily "drift" beyond my Content Place Holder. I'm trying to put a "footer" on every page of the site and this is
2
3735
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 page do NOT appear in any of the web forms located in my Secure dir, however, any forms off the root of my web app will display the master page with the graphics. Is there some trick to get master pages to work with forms authentication of forms...
17
2837
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 the Click event function of the Button execute or will the Click event function of the Button execute first & then will the Page_Load sub execute? Thanks, Arpan
6
3497
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, smaller ones, and referring to them in my HTML/ASP files on an as-needed basis. Essentially, I've organized things as: /default.asp
7
1969
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 help guide of an application I am working on. The help guide needs to be integrated into both a C# .NET desktop app AND a .NET based website using master pages. The requirements are that: (a) The same set of files are to be accessible through...
0
8349
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8275
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
8795
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
8695
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
8460
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
8576
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
4143
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
1
2696
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 we have to send another system
1
1906
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.