Scope of variables in C# script section of an ASP.NET page

P

Peter

One question that came up is that I am writing an ASP.NET page that has
some custom (C#) web controls. Since I am not afforded the ability to write
to the console for debugging/information purposes I thought I would simply
write things to a logfile. Well I instantantiated my custom C# class outside
the page_load function thinking it would only get constructed once and then
get skipped over on refreshes of the IE browser. However apparently there
seems to be no way around this. Basically I want to have the class as global
class that some of the other C# script functions can access.

My thinking was I could open up a TextWriter in the constructor of the
global
class and then close the file in the destructor. My thinking is flawed in a
couple
of points. I was thinking that the class would get instantiated once if it
was outside
of the page_load and the destructor would kick off if you left the page for
another
page. So my new tack is to open the file, insert my messages then quickly
close
it back up. Seem reasonable or is it doable to instantiate a class once upon
initially loading of a page and have it available throughout the C# script?

BTW, this C# scripting seems to be dissimilar to plain old C# applications.
I was trying
to call DateTime.Now but couldn't. Should I have available to me all the
functions
that are available in a plain old C# windows application? Are there any good
reference
materials on the C# (ASP.NET) scripting particulars that someone could
recommend?
Any information would be greatly appreciated!

Thanks,
Peter
 
V

ViewState

ASP.NET application are state less, they open and close in each post back,
so you won't have your variable set in the next time you do apost back.
I don't know if I got your scenario right but I think you need a session. so
you create the instance of your class, put in the values you want then you
put it in the session
 
D

Dave Hagerich

Yes you would need to do something like this.
When the control is created:

theControl = new MyControl();
Session["theControl"] = theControl;

then your page_load event needs something like this:

if(!IsPostBack)
{
if(Session["theControl"] != null)
{
theControl = (MyControl)Session["theControl"];
}
}

if you know theControl is created and stored on the first execution of the
page then you can skip the if that makes sure its not null. But as a habbit
I always check before grabbing a session variable just to make sure it is in
fact there.

Hope that helps,
Dave
 
P

Peter

Ah, excellent. I am just scratching the surface on Session variables and
I can now see how it can be used in this situation very nicely. Thanks!!!!

Dave Hagerich said:
Yes you would need to do something like this.
When the control is created:

theControl = new MyControl();
Session["theControl"] = theControl;

then your page_load event needs something like this:

if(!IsPostBack)
{
if(Session["theControl"] != null)
{
theControl = (MyControl)Session["theControl"];
}
}

if you know theControl is created and stored on the first execution of the
page then you can skip the if that makes sure its not null. But as a
habbit
I always check before grabbing a session variable just to make sure it is
in
fact there.

Hope that helps,
Dave
ViewState said:
ASP.NET application are state less, they open and close in each post
back,
so you won't have your variable set in the next time you do apost back.
I don't know if I got your scenario right but I think you need a session. so
you create the instance of your class, put in the values you want then
you
put it in the session
 

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

No members online now.

Forum statistics

Threads
473,999
Messages
2,570,246
Members
46,839
Latest member
MartinaBur

Latest Threads

Top