首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 网站开发 > asp.net >

一个简单的session有关问题

2012-04-22 
一个简单的session问题C# codeprotected void Button1_Click1(object sender, EventArgs e){Session[User

一个简单的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");
}

……

热点排行