CHUKS EKE
CHUKS EKE

Reputation: 71

How to declare a public variable and a public sub in an aspx web page?

How do declare a public variable .aspx web page that can be used in all the pages within my web application?

And/or create a Public Sub?

Upvotes: 1

Views: 10648

Answers (3)

Kira
Kira

Reputation: 628

Maybe you can work with ViewState to persist values into the session.

I think that isn't possible to create 'global variables', since the value is lost at every page load...

ViewState["key"] = variable;

variable = ViewState["key"];

Upvotes: 1

Alex Reitbort
Alex Reitbort

Reputation: 13706

You can use Session or Application state to hold global data needed for your website.

  • Session object is created per user and expires after some time(do not remember how much)
  • Application object is created when application started and will persist as long as application is running.

Upvotes: 0

Abel
Abel

Reputation: 57169

A public sub, assuming you use VB:

Public Sub MyOwnSub()
   'do something'
End Sub

A global variable: add a module to your VB and add a variable to that module. You can call that variable with ModuleName.VariableName from everywhere.

When you declare a public variable in the code-behind of your ASPX page, and you declare it shared, you can access it with YourPageName.VariableName from anywhere in your web application. Note that this variable is shared between all processes or requests.

Public Shared VariableName As String = "hello world"

Upvotes: 0

Related Questions