any event before OnLoad?

  • Thread starter Thread starter Daves
  • Start date Start date
D

Daves

can I have a user control with code running *before* the content pages'
OnLoad event? Or could I even have code in global.asax that is run before
OnLoad? Because I need to set some global variables and modify them before
the content page is run...
 
Hi Daves:

Application_BeginRequest in global.asax will execute before any of the
Load events. You could set up variables you need here (or pass
variables along in the Content.Items collection).
 
thx Scott
I could use Content.Items and I have tried but I don't like having to use
cast on it each time like
if ((int) Session("UserID") > 0)

so I am trying to have a global class containing my variables to be accessed
on all other content pages so that I can instead do
if (myClass.UserID > 0)

I did this with "public static" variables in my masterpage class but the
problem is I can't work with them (set their values) until after the
Page_OnLoad.

I even tried using the global class and accessed them using
if (ASP.Global_aspx.UserID >0)

but it turned out it was bound to the session so that they were never reset
between page cycles.


So I am completely stuck with this simple situation!
 
Hi Daves:

I understand, casting can be a pain - but you hvae to be careful with
static fields since there will be only one instance of the field for
the entire application. If you ever have multiple users with requests
executing at the same time they might go overwriting each other's
values.
 
Well, one approach is write a class that encapsulates where the data
is coming from, and does the casting before returning values to the
caller.

i.e.:

class MyContext
{
static int UserID
{
get
{
return (int)HttpContext.Current.Items["UserID"];
}

set
{
HttpContext.Current.Items["UserID"] = value;
}
}
}

The above could use some error checking code of course, but the nice
thing is then you'll never have to use typecasting in the rest of your
code, or hard coded strings, and you could always move a piece of
state from the Items collection to somewhere else (Session,
ViewState, Application, cookie) and not have to change the rest of
your code.
 
hmm that is actually a cool alternative! I still have echoes in my mind of
minimally using the Session items because they are heavy on resources...



Scott Allen said:
Well, one approach is write a class that encapsulates where the data
is coming from, and does the casting before returning values to the
caller.

i.e.:

class MyContext
{
static int UserID
{
get
{
return (int)HttpContext.Current.Items["UserID"];
}

set
{
HttpContext.Current.Items["UserID"] = value;
}
}
}

The above could use some error checking code of course, but the nice
thing is then you'll never have to use typecasting in the rest of your
code, or hard coded strings, and you could always move a piece of
state from the Items collection to somewhere else (Session,
ViewState, Application, cookie) and not have to change the rest of
your code.

--
Scott
http://www.OdeToCode.com/blogs/scott/

ouch! what is the alternative then if I really want to use class fields?
 
Back
Top