Reputation: 335
I build a web site that have a game. In the game the user need to see a label with a number and write the right number in text box and press check. When I open the site with 2 browsers (chrome and iexplorer), whats happen is that one continue from the second.
static int i = 0;
protected void Page_Load(object sender, EventArgs e)
{
number_lbl.Text = i + "";
}
protected void check_Click(object sender, EventArgs e)
{
if (Convert.ToInt16(textbox.Text) == i)
right_wrong_lbl.Text = "right";
else
right_wrong_lbl.Text = "wrong";
check.Enabled = false;
next.Visible = true;
}
protected void next_Click(object sender, EventArgs e)
{
i++;
check.Enabled = true;
next.Visible = false;
number_lbl.Text = i + "";
textbox.Text = "";
}
For example, I open the site in chrome and see "0", I write "0" and get "right" and click "next". I try again, see "1", write "1" and get "right" ans click "next". Now I open the site in iexplorer, see "0", write "0" and get "wrong", click "next" and see "4". If I wrote "3" in iexplorer I get "right". How can I do that the page for each player will be indepentent in the other players' pages?
Upvotes: 0
Views: 58
Reputation: 2185
Your user isn't in a static context. Try changing static int i to int i.
edit:
Sorry, I answered too quick.
As said in another answer here, you could store it as session variable: Try this (I don't have any way to test it right now, but it should work):
int i
{
get
{
if(Session["ClickCount"] == null)
{
Session["ClickCount"] = 0;
}
return int.Parse(Session["ClickCount"].ToString());
}
set
{
Session["ClickCount"] = value;
}
}
protected void Page_Load(object sender, EventArgs e)
{
number_lbl.Text = i + "";
}
protected void check_Click(object sender, EventArgs e)
{
if (Convert.ToInt16(textbox.Text) == i)
right_wrong_lbl.Text = "right";
else
right_wrong_lbl.Text = "wrong";
check.Enabled = false;
next.Visible = true;
}
protected void next_Click(object sender, EventArgs e)
{
i++;
check.Enabled = true;
next.Visible = false;
number_lbl.Text = i + "";
textbox.Text = "";
}
Upvotes: 0
Reputation: 30032
static
keyword before deceleration of i is causing this.
Store the value of i in a Session.
On Load:
if(Session["Number"]) == null)
{
Session["Number"] = 0;
number_lbl.Text = Session["Number"].ToString();
}
and replace each occurrence of i with Session["Number"]
.
Upvotes: 3