user1017882
user1017882

Reputation:

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

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?

Upvotes: 0

Views: 2358

Answers (3)

LiverpoolsNumber9
LiverpoolsNumber9

Reputation: 2394

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 ?

Upvotes: 0

gdoron
gdoron

Reputation: 150253

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...

Upvotes: 5

Marko
Marko

Reputation: 10992

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.

Upvotes: 0

Related Questions