开发者

How to declare a GLOBAL variable an aspx website?

How to declare a global variabl开发者_开发技巧e or a public sub in a web application that all aspx pages can have access to?


use a static variable in one of your code files.


"Global" variables can be kept in Cache using Cache.Add, or Application state using Application.Add.

"Globally-available" methods are generally an antipattern and should be avoided. If you need a utility function you can add a static method to a class, but beware the Ball of Mud antipattern.


1.You can use session variables which will be available to all pages in the scope of current session.

C#
Session("name")=value;

2.You can use application variables which will be available to entire application code untill application ends.

Application("name") = value;


Global.asax inherits from YourWebSiteApplicationClass...

public class YourWebSiteApplicationClass : HttpApplication
{
     public string GlobalVariable;
     public YourWebSiteApplicationClass()
     {
         GlobalVariable = "GLOBAL_VARIABLE";
     }
}

...and in any .aspx or .cs(.vb) file...

<% = ((YourWebSiteApplicationClass)this.ApplicationInstance).GlobalVariable %>

Return "GLOBAL_VARIABLE".


Create a PageBase class and have your pages inherit from it.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜