如何设置ASP.NET会话变量?

时间:2016-08-03 00:33:40

标签: c# asp.net session

我是ASP.NET的新手,我正在尝试设置会话变量。我有一个表单(SelectPlayer.aspx),我试图设置会话,但当我尝试在第二页上看到结果时,它没有显示任何值。以下是我的代码。

SelectPlayer.aspx

public partial class SelectPlayer : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (Session["player1"] == null)
        {   
            lblSelectPlayer.Text = "Select Player 1";
        }
    }

    protected void btnSelect_Click(object sender, EventArgs e)
    {            
        Session["player1"] = "PlayerSession";                
        Response.Redirect("Score.aspx");          
    }

}    

Score.aspx

    protected void Page_Load(object sender, EventArgs e)
    {
        if (Session["player1"] == null)
        {
            Response.Redirect("SelectPlayer.aspx");
        }  
    }

1 个答案:

答案 0 :(得分:-2)

<!-- Chat --> <div class="container"> <div class="row"> <div class="col-md-8 col-md-offset-2"> <div class="panel container-fluid"> <center><h4><b> Chat: </b></h4></center> <div class="well" > <div class="loader"></div> <script> $(document).ready(function () { $('#msg').change(function () { if ($.trim($('#msg').val()).length < 1) { $('#enviar').addClass('disabled'); } else { $('#enviar').removeClass('disabled'); //No guarantee it isn't mindless gibberish, sorry. } }); }); </script> <script type="text/javascript" language="javascript"> function msgsend() { var msg = $("#msg").val(); if (msg!='') { $('#msg').val(""); $.post("enviar.php", { msg: msg }, } } </script> <div id="chat"> <div id="status" style="display: none;"></div> </div> </div> <center> <form id="ajax" role="form" method="post"> <div class="form-group"> <textarea id="msg" type="text" name="chat" class="form-control" placeholder="Digite aqui..."></textarea> </div> </form> <button id="enviar" name="submit" class="btn btn-success disabled"> Enviar </button> </center> </div> </div> </div> </div> 更改为Response.Redirect("Score.aspx");

重定向的第二个参数表示您要结束响应。我在过去发现,在没有第二个参数的情况下进行重定向之前直接设置会话可能会导致问题。