从会话中检索值. [英] to retrieve values from session.

查看:66
本文介绍了从会话中检索值.的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

查看state.cs文件

view state.cs file

protected void Button1_Click(object sender, EventArgs e)
      {
          Dictionary<string, string> dictionary = new Dictionary<string, string>();
          dictionary.Add("txtUserName", txtUserName.Value);
          dictionary.Add("txtPassword", txtPassword.Value);
          ViewState["User"] = dictionary;
          Session["Dictionary"] = dictionary;
          Response.Redirect("Session.aspx");
      }


session.cs文件


session.cs file

protected void Button1_Click(object sender, EventArgs e)
        {
            var user = ViewState["User"];

        }




我在viewstate.cs页面中输入了2个值,并将这些值存储在session和viewstate中,然后重定向到session.cs页面.
现在,我想从session.cs页面中的会话中读取值.




I have entered 2 values in viewstate.cs page and stored the values in session and viewstate and redirected to session.cs page.
now,i want to read the values from the session in session.cs page.
How can i do it?

推荐答案

以下是如何从会话变量中检索值的方法.

The following is the way how you can retrieve values from a session variable.

Dictionary<string,> variableName = (Dictionary<string,>)Session["YourSessionVariableName"];



注意:不要在Session.Abandon()/Remove()等之后执行.否则,可能会发生异常)

编辑MB:已更改为将会话转换为词典对象.



Note: Do not perform after Session.Abandon()/Remove() etc. Otherwise you can expect an Exception)

EDIT MB : Changed to cast session as dictionary object.


if(Session["Dictionary"] != null)
{
Dictionary<string,string> Dictionary1 = ((Dictionary<string,string>)Session["Dictionary"];
}
else
{
//
}


类似于dictionary = Session["Dictionary"];的代码应该可以使用.
Code similar to dictionary = Session["Dictionary"]; should work.


这篇关于从会话中检索值.的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆