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.
我建立了一个有游戏的网站。在游戏中,用户需要看到带有数字的标签,并在文本框中写入正确的数字并按下检查。当我用2个浏览器(chrome和iexplorer)打开网站时,发生的是从第二个浏览器继续。
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?
例如,我在chrome中打开网站并看到“0”,我写“0”并获得“正确”并点击“下一步”。我再试一次,看“1”,写“1”,然后“右”,点击“下一步”。现在我在iexplorer中打开网站,看到“0”,写“0”并得到“错误”,点击“下一步”并看到“4”。如果我在iexplorer中写了“3”,我就会“正确”。我怎么能这样做,每个玩家的页面将在其他玩家的页面中独立?
2 个解决方案
#1
3
static
keyword before deceleration of i is causing this. Store the value of i in a Session.
在减速之前的静态关键字导致了这一点。将i的值存储在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"]
.
并用会话[“数字”]替换每次出现的i。
#2
0
Your user isn't in a static context. Try changing static int i to int i.
您的用户不在静态环境中。尝试将static int i更改为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 = "";
}
#1
3
static
keyword before deceleration of i is causing this. Store the value of i in a Session.
在减速之前的静态关键字导致了这一点。将i的值存储在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"]
.
并用会话[“数字”]替换每次出现的i。
#2
0
Your user isn't in a static context. Try changing static int i to int i.
您的用户不在静态环境中。尝试将static int i更改为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 = "";
}