代码如下,第一页保存了一个Session["yonghuming"],第二页 yhm = Session["yonghuming"].ToString();却没有获得原Session的值。请高手赐教!
注:把第二页的 yhm = Session["yonghuming"].ToString();换成yhm = "aaa"; 时会有反应,说明已经进行到这一步了,只是取不到原Session的值。
第一页:public partial class web_login : System.Web.UI.Page
{
......
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
}
}
protected void ImageButton1_Click(object sender, ImageClickEventArgs e)
{
if (this.txtCode.Text.ToString() == Request.Cookies["CheckCode"].Value.ToString())
{
if (bll.CheckLogin(Number, password))
{
if (bc.userLimit(Number))
{
//将用户名保存到session
Session["yonghuming"] = this.txtNumber.ToString();
Session.Timeout = 600; }
}
}
}
}
第二页:public partial class web_option : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
.....
}
}
protected void Repeater3_ItemDataBound(object sender, RepeaterItemEventArgs e)
{
int jibie = Convert.ToInt32(Session["jibie"]);
String yhm = "";
if (Session["yonghuming"] != null)
{
yhm = Session["yonghuming"].ToString();
}
......
}
}
注:把第二页的 yhm = Session["yonghuming"].ToString();换成yhm = "aaa"; 时会有反应,说明已经进行到这一步了,只是取不到原Session的值。
第一页:public partial class web_login : System.Web.UI.Page
{
......
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
}
}
protected void ImageButton1_Click(object sender, ImageClickEventArgs e)
{
if (this.txtCode.Text.ToString() == Request.Cookies["CheckCode"].Value.ToString())
{
if (bll.CheckLogin(Number, password))
{
if (bc.userLimit(Number))
{
//将用户名保存到session
Session["yonghuming"] = this.txtNumber.ToString();
Session.Timeout = 600; }
}
}
}
}
第二页:public partial class web_option : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
.....
}
}
protected void Repeater3_ItemDataBound(object sender, RepeaterItemEventArgs e)
{
int jibie = Convert.ToInt32(Session["jibie"]);
String yhm = "";
if (Session["yonghuming"] != null)
{
yhm = Session["yonghuming"].ToString();
}
......
}
}
如果状态是sessionState mode="InProc" 容易丢失,你把状态改成mode="StateServer",试试看
这个执行到了么?
{
yhm = Session["yonghuming"].ToString();
}
你是说把上面的换成
if (Session["yonghuming"] != null)
{
yhm = "aaa";
}
yhm = "aaa";会被执行么?
如果是的话,那就是Session["yonghuming"]为""
因为if过了
yhm = Session["yonghuming"].ToString();
改成
yhm = "aaa";后
yhm = "aaa";被执行了。
说明Session["yonghuming"]被赋值过了,因为Session["yonghuming"]不是null.看看你有没有其他地方给Session["yonghuming"]赋值,还有在前面页面Session["yonghuming"] = this.txtNumber.ToString(); 设置断点, 看看Session["yonghuming"]是什么。