Basic Q: Page variables

J

Jimmy

This is a basic question. Does Page related member variable values
only be set in the Page they are executed? Example code like below

public partial class _Default : System.Web.UI.Page
{
string var = "";

protected void BtnSubmit_Click(object sender, EventArgs e)
{
if (Select.Checked)
{
var = "Visited";
tbox.Text = "Selected On " + var;
}
else
tbox.Text = "Selected Off " + var;
}


Variable var is set to value 'Visited' ONLY in web page that is executed by
submit.
Not in other instances of same page executed in other page instances in
other Browser instances.

What I like to know that does Page instance differ in this case from Java's
Servlet member variables
that are shared with all instances.

Cheers!
 
M

Marina Levit [MVP]

The page class is like any other class. Any variables defined in it, are
tied to an instance of the class. If you wanted to have the variable be
shared among all instance, you would have to declare it as 'static'.
 
J

John Saunders

Marina Levit said:
The page class is like any other class. Any variables defined in it, are
tied to an instance of the class. If you wanted to have the variable be
shared among all instance, you would have to declare it as 'static'.

It would then require synchronization to prevent it being updated by
multiple threads simultaneously.

Writable static member variables are usually a bad idea in web pages.

John
 

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,774
Messages
2,569,598
Members
45,161
Latest member
GertrudeMa
Top