一个简单的session问题

yigeming2008 2012-04-07 11:24:29

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

protected void Button2_Click(object sender, EventArgs e)
{
string usertype = Session["UserType"].ToString();
Response.Write(usertype);
}

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

...全文
98 14 打赏 收藏 转发到动态 举报
写回复
用AI写文章
14 条回复
切换为时间正序
请发表友善的回复…
发表回复
hwbox 2012-04-20
  • 打赏
  • 举报
回复
[Quote=引用 13 楼 的回复:]

解决了,原来如果把浏览器的cookie全部禁用的话,session也不能用了。
[/Quote]

全禁.....
yigeming2008 2012-04-20
  • 打赏
  • 举报
回复
解决了,原来如果把浏览器的cookie全部禁用的话,session也不能用了。
0c0c0f 2012-04-07
  • 打赏
  • 举报
回复
未将对象引用设置到对象的实例是session对象为空的原因
子在川上笑 2012-04-07
  • 打赏
  • 举报
回复
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.SessionID,然后在第二个代码异常中断时去调试一下这个SessionID值(通过监视窗口或者即时命令窗口),看看是不是还是原来的值。

子在川上笑 2012-04-07
  • 打赏
  • 举报
回复
[Quote=引用 5 楼 的回复:]

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

汗。。!
子在川上笑 2012-04-07
  • 打赏
  • 举报
回复
string usertype = Session["UserType"].ToString();
如果要加给字体加颜色的话→<font style='color:#FF0000'>usertype</font>
然后: Response.Write("<font style='color:#FF0000'>usertype</font>");
yigeming2008 2012-04-07
  • 打赏
  • 举报
回复
我晕,[color]是csdn的ubb标签
子在川上笑 2012-04-07
  • 打赏
  • 举报
回复
string usertype = Session["UserType"].ToString(); 把两边的[color]去掉吧!
子在川上笑 2012-04-07
  • 打赏
  • 举报
回复
一楼正解!
yigeming2008 2012-04-07
  • 打赏
  • 举报
回复
我要先点了button2 的话,我就不来问这个问题了。tostring,因为已经赋值了,只是测试,我才直接这么用的。session["usertype"]一直就是null,所以我才来问这个问题。
wtnu200 2012-04-07
  • 打赏
  • 举报
回复
你先点了button2吧?如果不存在这个session,你用tostring,就会出现问题。
gudujianxiao 2012-04-07
  • 打赏
  • 举报
回复
[Quote=引用 9 楼 的回复:]

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

……
[/Quote]
+1

62,268

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术交流专区
javascript云原生 企业社区
社区管理员
  • ASP.NET
  • .Net开发者社区
  • R小R
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

.NET 社区是一个围绕开源 .NET 的开放、热情、创新、包容的技术社区。社区致力于为广大 .NET 爱好者提供一个良好的知识共享、协同互助的 .NET 技术交流环境。我们尊重不同意见,支持健康理性的辩论和互动,反对歧视和攻击。

希望和大家一起共同营造一个活跃、友好的社区氛围。

试试用AI创作助手写篇文章吧