Can I use static variable in the scope of a PAGE?

  • Thread starter Thread starter Sandeep Singh
  • Start date Start date
S

Sandeep Singh

I m building an application in which I redirect the user to a particular
page. In the Page Load event of the that page , I assign a variable
some value. Then I call the Button click event on that page. I have
applied the !IsPostBack so that the Page Load event does not occur on
clicking the button. Now the problem is that I want to use the value of
that variable which i was population on Page Load event.
Can I use that variable as static?
I have a doubt in my mind that as I am simply redirecting the users to
that page, the value of that variable could be set to some another value
if another user is redirected to that page as the variable is static.
Please help me to clarify my concept.


Thanks,
Sandeep
 
A static variable in a class is shared by all instances of that class. If
you want to persist the variable, put it into ViewState.

--
HTH,

Kevin Spencer
Microsoft MVP
Professional Numbskull

Hard work is a medication for which
there is no placebo.
 
Well, there are several caching mechanisms available, but if it is
apage-scoped variable of a primitive type, the most efficient place would be
the ViewState. I didn't want to confuse the OP at this point (unless it
became necessary!)

--
HTH,

Kevin Spencer
Microsoft MVP
Professional Numbskull

Hard work is a medication for which
there is no placebo.
 
Hi Sandeep,

I agree with other members' suggestion. Static variables in .net is shared
based on AppDomain scope, and as for ASP.NET application, each application
running inside its own AppDomain , that means static variables are
appication wide variables. So for your page level code, I suggest you
considering using other storage like SessionState, or the Profile Service...

Regards,

Steven Cheng
Microsoft Online Community Support


==================================================

When responding to posts, please "Reply to Group" via your newsreader so
that others may learn and benefit from your issue.

==================================================


This posting is provided "AS IS" with no warranties, and confers no rights.



Get Secure! www.microsoft.com/security
(This posting is provided "AS IS", with no warranties, and confers no
rights.)
 

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

Back
Top