我对ASP.NET非常陌生,所以我很想在这里找到一些简单的东西,但是我的Session遇到了问题。当我点击" NewTurn"按钮第一次,它就像它应该的那样创建玩家,然而,当我再次点击它时,不知怎的,所有的统计数据又回来了,就好像它再次被创建一样(统计数据如"健康"回到最大但是我可以在调试模式中看到它只进入" if == null"功能ONCE。
我不明白我做错了什么,这是代码:
protected void NewTurn_Click(object sender, ImageClickEventArgs e)
{
StartButton.Visible = false;
RulesLink.Visible = false;
NewTurnButton.Visible = true;
if (Session["PrevObject"] == null)
{
Character player = new Character(1);
Session["PrevObject"] = player;
}
Character prevObj = (Character)Session["PrevObject"];
prevObj = CreateCard(prevObj);
Session["PrevObject"] = prevObj;
}
编辑:我发现了错误并且它与Session无关,我只是返回了对象而没有在发送回来之前设置正确的值。谢谢你的建议,我很抱歉浪费你的时间!
答案 0 :(得分:1)
请检查您的代码中的评论
protected void NewTurn_Click(object sender, ImageClickEventArgs e)
{
StartButton.Visible = false;
RulesLink.Visible = false;
NewTurnButton.Visible = true;
if (Session["PrevObject"] == null)
{
Character player = new Character(1);
Session["PrevObject"] = player;
}
Character prevObj = (Character)Session["PrevObject"];
// I think here you made mistake
prevObj = CreateCard(prevObj);
// check here again using DEbug point
// and make sure prevObj not null
Session["PrevObject"] = prevObj;
}
答案 1 :(得分:0)
你应该提到播放器的属性将分配给Session [“PrevObject”]。 假设我有像
这样的课程public class Character
{
public playtime{get;set;}
Character(int i)
{
playtime=i;
}
}
现在我将按照您的提及进行编码:
protected void NewTurn_Click(object sender, ImageClickEventArgs e)
{
StartButton.Visible = false;
RulesLink.Visible = false;
NewTurnButton.Visible = true;
if (Session["PrevObject"] == null)
{
Character player = new Character(1);
//Here is a little change you will have to specify what shuold be assign to the Session //variable
Session["PrevObject"] = player.playtime;
}
希望这会对你有所帮助。 如果您觉得有帮助,请投票 谢谢,我们总是感激不尽。