简体   繁体   中英

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?

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

为什么不创建主题颜色的单独类,并在global.asax的application-start事件中将主题颜色设置为某种值。

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 ?

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM