473,387 Members | 1,575 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

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

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
Nov 19 '05 #1
3 1742
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

"Peter" <sp**@nospamerino.com> escreveu na mensagem
news:K5********************@comcast.com...
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

Nov 19 '05 #2
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" <oa*****@ig.com.br> wrote in message
news:eK**************@TK2MSFTNGP14.phx.gbl...
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

"Peter" <sp**@nospamerino.com> escreveu na mensagem
news:K5********************@comcast.com...
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


Nov 19 '05 #3
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" <dh*******@arclighte.com> wrote in message
news:uv**************@tk2msftngp13.phx.gbl...
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" <oa*****@ig.com.br> wrote in message
news:eK**************@TK2MSFTNGP14.phx.gbl...
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

"Peter" <sp**@nospamerino.com> escreveu na mensagem
news:K5********************@comcast.com...
> 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
>



Nov 19 '05 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

33
by: aa | last post by:
I am migrating to PHP from ASP where there are the Application Scope variables which are accessible from any page on a website and which are used, in particular, for hit counters. Is there a similar...
8
by: lawrence | last post by:
I'm learning Javascript. I downloaded a script for study. Please tell me how the variable "loop" can have scope in the first function when it is altered in the second function? It is not defined...
7
by: Michael G | last post by:
I am a little surprised that the following that $x is visible outside of the scope in which it is (?)defined(?) (not sure if that is the correct term here). I am trying to find where in the php...
10
by: Not Available | last post by:
On the host server: namespace JCart.Common public class JCartConfiguration : IConfigurationSectionHandler private static String dbConnectionString; public static String ConnectionString { get...
4
by: Arpan | last post by:
The following code works fine: <script runat="server"> Dim strName As String = "Arpan" Sub Page_Load(ByVal obj As Object, ByVal ea As EventArgs) Page.DataBind() End Sub </script> <form...
5
by: shawn.ready | last post by:
Hi All I have a little page I am having issues with. What I am doing is using php to query a odbc database and create a Google Map with point information I get back from the db. But the...
5
by: Trev | last post by:
Hi all, Are variables in javascript local to the page, or to the <scripttags they are defined within? Say I had a bit of code as follows: <HEAD> <script> var myVar = MyFunction();
1
pbmods
by: pbmods | last post by:
VARIABLE SCOPE IN JAVASCRIPT LEVEL: BEGINNER/INTERMEDIATE (INTERMEDIATE STUFF IN ) PREREQS: VARIABLES First off, what the heck is 'scope' (the kind that doesn't help kill the germs that cause...
5
by: chromis | last post by:
Hi there, I've recently been updating a site to use locking on application level variables, and I am trying to use a commonly used method which copies the application struct into the request...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.