how to expose a variable with global.asax?

D

D.

Hi,

I need to expose a variable to every aspx file of the applicationroot.
I tried with global.asax like this:
<%@ Application Language="VB" %>
<script runat="server">
public a as string
Sub Application_Start(ByVal sender As Object, ByVal e As EventArgs)
a="ok"
End Sub
</script>

When running an aspx-file in the applicationroot with this code:
<%@ Page Language="VB" %>
<%@ import namespace ="System.Web.UI.Page" %>
<% Response.Write(a)%>

i get the error: "Name 'a' is not declared"

Thanks for help
Dominique
 
K

Karl Seguin [MVP]

Since ur hooking into Application_Start, I'll assume this is a shared
variable for all users of your site. If so, the simplest solution is to
store it in the application object:
Application.Add("SomeKey", "ok")

which you can then retrieve via:
cstr(Application("SomeKey"))


If this is a per-user/request, hook into the Application's BeginRequest and
store it in the Context.Items collection..

Karl
 
P

Patrice

My personal preference would be to create my own class (possibly exposing
Items in a strongly type manner as explained by Karl).

This way you don't have to cast here an there from the exposed application
object plus it's likely not related to the application in the IIS sense but
ather from business point fo view...
 
D

D.

Karl,

I tried what you proposed and it works.
But i also tried with cookies.
What would you advice to use?
Thanks



"Karl Seguin [MVP]" <karl REMOVE @ REMOVE openmymind REMOVEMETOO . ANDME
net> wrote in message news:[email protected]...
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,743
Messages
2,569,478
Members
44,898
Latest member
BlairH7607

Latest Threads

Top