Solved

How can you have a MasterPage inherit a BasePage?

Posted on 2009-05-11
2
467 Views
Last Modified: 2013-11-07
Hi,

I'm fresh to .NET 2.0 (started today) so please be patient with me! :) .

I have a masterpage and have completed the layout.
I created a BasePage and have set some variables.
I have a content (default.aspx) page and its inheriting from the BasePage.

The content page can read the variables from the BasePage.
The MasterPage cannot read the values: "CS0103: The name 'config__path_js' does not exist in the current context".

How can I get the masterpage to inherit from BasePage? I've been looking on the net and apparently you can't but there must be a way to achieve this.

In general, I want to have one file that contains variables which is fed from an XML file.
I want to be able to access these variables from Masterpage, content pages and user controls.

How can I do this?
App_Code/BasePage.cs
~~~~~~~~~~~~~~~~~~~~
using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Xml;
 
/// <summary>
/// This BasePage is used to have content pages inherit these variables
/// </summary>
public class BasePage : System.Web.UI.Page
{
 
    public String config__path_root;
    public String config__path_img;
    public String config__path_swf;
    public String config__path_flv;
    public String config__path_css;
    public String config__path_js;
    public String config__path_pdf;
 
    protected override void OnInit(EventArgs e)
   {
       XmlDocument doc = new XmlDocument();
       doc.Load(Server.MapPath("~/config.xml"));
       XmlNode root = doc.DocumentElement;
 
       config__path_root = root.SelectSingleNode("path_root").ChildNodes[0].Value;
       config__path_img = root.SelectSingleNode("path_img").ChildNodes[0].Value;
       config__path_swf = root.SelectSingleNode("path_swf").ChildNodes[0].Value;
       config__path_flv = root.SelectSingleNode("path_flv").ChildNodes[0].Value;
       config__path_css = root.SelectSingleNode("path_css").ChildNodes[0].Value;
       config__path_js = root.SelectSingleNode("path_js").ChildNodes[0].Value;
       config__path_pdf = root.SelectSingleNode("path_pdf").ChildNodes[0].Value;
   }
 
}
 
 
 
Default.aspx.cs
~~~~~~~~~~~~~
using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
 
public partial class _Default : BasePage 
{
    protected void Page_Load(object sender, EventArgs e)
    {
        
    }
}
 
 
 
MasterPage.master.cs
~~~~~~~~~~~~~~~~~~
using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
 
public partial class MasterPage : System.Web.UI.MasterPage
{
    protected void Page_Load(object sender, EventArgs e)
    {
    }
}

Open in new window

0
Comment
Question by:bloodtrain
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
2 Comments
 
LVL 12

Accepted Solution

by:
williamcampbell earned 500 total points
ID: 24356839
You don't have to inherit you could make the variables static

    public static String config__path_root;
    public static String config__path_img;

 
   Then access them

    BasePage.config__path_root;

  Also the usual way to store config variables is in web.config using ConfigurationManager

  Take a look at that it offers global access to config variables.
0
 
LVL 2

Author Comment

by:bloodtrain
ID: 24357943
Thanks williamcampbell for your reply. I didn't want to use the web.config because I need to use some programming logic as well when declaring the variables. This seems to work great so thanks a lot!
0

Featured Post

Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
PowerShell: ForEach-Object Export to CSV 4 92
Set arraylist as datatable 2 30
What!!!??? 5 63
Delete command SQLdatasource in asp.net, vb 2 28
For those of you who don't follow the news, or just happen to live under rocks, Microsoft Research released a beta SDK (http://www.microsoft.com/en-us/download/details.aspx?id=27876) for the Xbox 360 Kinect. If you don't know what a Kinect is (http:…
Real-time is more about the business, not the technology. In day-to-day life, to make real-time decisions like buying or investing, business needs the latest information(e.g. Gold Rate/Stock Rate). Unlike traditional days, you need not wait for a fe…
Finds all prime numbers in a range requested and places them in a public primes() array. I've demostrated a template size of 30 (2 * 3 * 5) but larger templates can be built such 210  (2 * 3 * 5 * 7) or 2310  (2 * 3 * 5 * 7 * 11). The larger templa…

752 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question