login.aspx页面
取Session值代码如下:Session["zmadmin"]=Label1.Text.ToString();Session["zmpwd"]=Label2.Text.ToString();Session["zmpopid"]=Label3.Text.ToString();Response.Write(Session["zmadmin"].ToString()); (确定可以在字幕上打印出结果并且正确)Response.Redirect("test.aspx");
test.aspx页面
private void Page_Load(object sender, System.EventArgs e)
{
    if(Session["zmadmin"] != null)
      {
        Label1.Text=Session["zmadmin"].ToString();
      }
    else
     {
        Label1.Text="Session值为空";
      }
}屏幕上打印出:Session值为空
我确定在web.config里的设置正确,为什么会出现这样的情况,请帮帮小弟,谢谢

解决方案 »

  1.   

    这是之前的代码:using System;
    using System.Collections;
    using System.ComponentModel;
    using System.Data;
    using System.Drawing;
    using System.Web;
    using System.Web.SessionState;
    using System.Web.UI;
    using System.Web.UI.WebControls;
    using System.Web.UI.HtmlControls;
    using System.Drawing.Imaging;
    using System.Drawing.Drawing2D;
    using System.Data.OleDb;
    using System.Web.Security;namespace ANJA.admin_login
    {
    /// <summary>
    /// WebForm1 的摘要说明。
    /// </summary>
    public class WebForm1 : System.Web.UI.Page
    {
    protected System.Web.UI.WebControls.Button Button1;
    protected System.Web.UI.WebControls.TextBox password;
    protected System.Web.UI.WebControls.RequiredFieldValidator RequiredFieldValidator2;
    protected System.Web.UI.WebControls.Button Button2;
    protected System.Web.UI.WebControls.Image Image1;
    protected System.Web.UI.WebControls.RequiredFieldValidator RequiredFieldValidator1;
    protected System.Web.UI.HtmlControls.HtmlForm Form1;
    protected System.Web.UI.WebControls.Label Message;
    protected System.Web.UI.WebControls.TextBox adminname;
    protected System.Web.UI.WebControls.TextBox getimage;
    protected System.Web.UI.WebControls.RegularExpressionValidator chk_userid02;
    protected System.Web.UI.WebControls.RegularExpressionValidator chk_userid03;
    protected System.Web.UI.WebControls.RequiredFieldValidator RequiredFieldValidator4;
    config obj = new config();
    OleDbCommand mycommand;
    OleDbDataReader rs;
    private void Page_Load(object sender, System.EventArgs e)
    {
    // 在此处放置用户代码以初始化页面
    if(!this.IsPostBack)
    {
    //图片验证码的代码
    //建立位图对象
    Bitmap newBitmap = new Bitmap(36,16,PixelFormat.Format32bppArgb);
    //根据上面创建的位图对象创建绘图面
    Graphics g = Graphics.FromImage(newBitmap);
    //以指定的颜色填充矩形区
    g.FillRectangle(new SolidBrush(Color.White), new Rectangle(0,0,38,19));
    //创建字体对象
    Font textFont = new Font("Times New Roman",10);
    //创建RectangleF结构指定一个区域
    RectangleF rectangle = new RectangleF(0,0,38,19);
    //创建随机数对象
    Random rd = new Random();
    //取得随机数
    int valationNo = 1000 + rd.Next(8999);
    //使用指定的颜色填充上面RectangleF结构指定的矩形区域
    g.FillRectangle(new SolidBrush(Color.White), rectangle);
    //在上面填充的矩形区域中填充上面生成的随机数
    g.DrawString(valationNo.ToString(), textFont, new SolidBrush(Color.Red), rectangle);
    //把创建的位图保存到指定的路径
    newBitmap.Save(Server.MapPath("images")+"\\VaImg.gif", ImageFormat.Gif);
    //图片验证码代码完毕 Session["getint"]=valationNo.ToString();
    }

    }

    #region Web 窗体设计器生成的代码
    override protected void OnInit(EventArgs e)
    {
    //
    // CODEGEN: 该调用是 ASP.NET Web 窗体设计器所必需的。
    //
    InitializeComponent();
    base.OnInit(e);
    }

    /// <summary>
    /// 设计器支持所需的方法 - 不要使用代码编辑器修改
    /// 此方法的内容。
    /// </summary>
    private void InitializeComponent()
    {    
    this.Button1.Click += new System.EventHandler(this.Button1_Click);
    this.Button2.Click += new System.EventHandler(this.Button2_Click);
    this.Load += new System.EventHandler(this.Page_Load); }
    #endregion private void Button1_Click(object sender, System.EventArgs e)
    {
    if(Page.IsValid)
    {
    if (getimage.Text.ToString().Trim() != Session["getint"].ToString())
    {

    Message.Text = "请输入正确有验证码!";
    }
    else
    {
    string a_userpassword = System.Web.Security.FormsAuthentication.HashPasswordForStoringInConfigFile(password.Text,"md5").ToLower();
    obj.open();
    mycommand=new OleDbCommand("select * from [admin] where adminname='"+ adminname.Text +"'",obj.myconn);
    rs=mycommand.ExecuteReader();
    if(!rs.Read())
    {

    obj.myconn.Close();
    Message.Text = "只有相关人员才能进入,请确定你的身份!!";
    return;
    }
    else
    {
    if(a_userpassword != rs["password"].ToString())
    {
    obj.myconn.Close();
    Message.Text = "对不起,你输入有错误,如果一直出现这样的问题,请与管理员联系!!";
    return;
    }
    else
    {

    Session["zmadmin"]=rs["adminname"].ToString();
    Session["zmpwd"]=rs["password"].ToString();
    Session["zmpopid"]=rs["popedom"].ToString();
    obj.myconn.Close();
    //Response.Write(Session["zmadmin"].ToString());
    Response.Redirect("test.aspx");
    }
    rs.Close();
    }
    mycommand.Dispose();
    obj.myconn.Close();
    obj.myconn.Dispose();
    }
    }
    } private void Button2_Click(object sender, System.EventArgs e)
    {
    adminname.Text="";
    password.Text="";
    getimage.Text=""; }
    }
    }
      

  2.   

    把web.config中的cookieless="false",改为true试一下看看
       如果可以的话就是IE隐私被设置
      

  3.   

    Session["zmadmin"]=rs["adminname"].ToString();看看这里的值rs["adminname"].ToString();其它没什么问题呀
      

  4.   

    一般不会出现这样的问题。
    Session.Timeout的时间是多少
    private void Page_Load(object sender, System.EventArgs e)
    {
      Response.Write(Session["zmadmin"].ToString());
    }
    这样的结果如何
      

  5.   

    如果是这样的话:会提示
    private void Page_Load(object sender, System.EventArgs e)
    {
      Response.Write(Session["zmadmin"].ToString());
    }
    未将对象引用设置到对象的实例。 
    说明: 执行当前 Web 请求期间,出现未处理的异常。请检查堆栈跟踪信息,以了解有关该错误以及代码中导致错误的出处的详细信息。 异常详细信息: System.NullReferenceException: 未将对象引用设置到对象的实例。源错误: 
    行 21:  private void Page_Load(object sender, System.EventArgs e)
    行 22:  {
    行 23:  Response.Write(Session["zmadmin"].ToString());
    行 24:  }
    行 25: 
     
      

  6.   

    能试的办法我都试过了。唉~~~~
    怎么回事啊
    <sessionState 
                mode="InProc"
                stateConnectionString="tcpip=127.0.0.1:42424"
                sqlConnectionString="data source=127.0.0.1;Trusted_Connection=yes"
                cookieless="false" 
                timeout="20" 
        />
    这个也没问题啊
      

  7.   

    可能是你的浏览器禁用cookie了.
      

  8.   

    实在不行,建议楼主把Session改为SQL Server存储吧,还有问题,就将状态服务器部署到其他计算机上...
      

  9.   

    我终于找到答案了,现在结贴。
    我把数据库放到BIN目录下面,从而导致Session和Application不断丢失
      

  10.   

    就是数据库一定不能放在Bin目录下,只要放到Bin目录下就会导致Session和Application不断丢失