How do I reference a property in Global.asax from .aspx page?

2.3k views Asked by At

The name 'Global' does not exist in the current context

I'm getting the above error when trying to reference a property I've created in Global.asax:

public static String ThemeColor
{ get; set; }

from the C# on the aspx page (outputting some javascript):

alert("<%=Global.ThemeColor %>");

Any ideas why?

3

There are 3 answers

0
gdoron On BEST ANSWER

Several options:

  • The class name isn't Global, Maybe you changed it?
  • You are missing the using of the namespace

You really should not use the Global.asax to handle the theme color.
css seems to be a more appropriate place for it...

0
marko On

Why don't you make a separate class of the theme-color and at the application-start event in global.asax set the themecolor to something.

0
LiverpoolsNumber9 On

If you're putting these sorts of values in Global.asax you need a doctor.

Create a class called "GlobalSiteValues" or whatever. Make sure the namespace it lives in is either the same as the aspx page, or registered in web.config (or non-existent or use the full name).

Then this will work (once you have set the value, obviously)

public class GlobalSiteValues
{
    public static string MyString{ get;set }
    public static int MyInt{ get;set; }
}

... and in the aspx page (in script block)...

var abc = "<%= GlobalSiteValues.MyString %>";
alert(abc);

Or why not set up a "context class" for your site. Like HttpContext.Current ?