当前位置: 代码迷 >> ASP.NET >> 一个简单的session有关问题
  详细解决方案

一个简单的session有关问题

热度:3576   发布时间:2013-02-25 00:00:00.0
一个简单的session问题
C# code
protected void Button1_Click1(object sender, EventArgs e)        {            Session["UserType"] = "OK";            Response.Write("OK");        }        protected void Button2_Click(object sender, EventArgs e)        {            [color=#FF0000]string usertype = Session["UserType"].ToString();[/color]            Response.Write(usertype);        }

两个按钮的方法
一个给session复制,一个获取值
但是为什么会提示未将对象引用设置到对象的实例。 
是不是设置的问题。
从来没遇到过,所以上来提问。



------解决方案--------------------------------------------------------
你先点了button2吧?如果不存在这个session,你用tostring,就会出现问题。
------解决方案--------------------------------------------------------
一楼正解!
------解决方案--------------------------------------------------------
string usertype = Session["UserType"].ToString(); 把两边的[color]去掉吧!
------解决方案--------------------------------------------------------
string usertype = Session["UserType"].ToString();
如果要加给字体加颜色的话→<font style='color:#FF0000'>usertype</font>
然后: Response.Write("<font style='color:#FF0000'>usertype</font>");
------解决方案--------------------------------------------------------
探讨

我晕,[color]是csdn的ubb标签

------解决方案--------------------------------------------------------
呵呵。

不知道什么原因。在点击两个按钮之间还有其它什么操作吗?你贴出来的代码是全部代码吗?

你可以在第一个代码中打印一下Session.SessionID,然后在第二个代码异常中断时去调试一下这个SessionID值(通过监视窗口或者即时命令窗口),看看是不是还是原来的值。


------解决方案--------------------------------------------------------
protected void Button1_Click1(object sender, EventArgs e)
{
Session["UserType"] = "OK";
Response.Write("OK");
}

protected void Button2_Click(object sender, EventArgs e)
{
if(!string.IsNullOrEmpty(Session["UserType"]))//如果Session不为空
{
string usertype = Session["UserType"].ToString();
Response.Write(usertype);
}

}
------解决方案--------------------------------------------------------
未将对象引用设置到对象的实例是session对象为空的原因

------解决方案--------------------------------------------------------
探讨

protected void Button1_Click1(object sender, EventArgs e)
{
Session["UserType"] = "OK";
Response.Write("OK");
}

……
  相关解决方案