我的Session为什么老是为NULL?
开发环境VS2005,想做一个验证码。
using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Drawing;
using System.Drawing.Imaging;
public partial class ValidateCode : System.Web.UI.Page
{
private readonly string ImagePath = "Validator.jpg ";
private string gifNubic = " ";
protected void Page_Load(object sender, EventArgs e)
{
gifNubic = GetRandomint ();
Session[ "test "] = gifNubic;
///创建Bmp位图
Bitmap bitMapImage = new System.Drawing.Bitmap(Server.MapPath(ImagePath));
Graphics graphicImage = Graphics.FromImage(bitMapImage);
///设置画笔的输出模式
graphicImage.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.HighSpeed;
///添加文本字符串
graphicImage.DrawString(gifNubic, new Font( "Arial ", 10, FontStyle.Bold), SystemBrushes.WindowText, new Point(0, 0));
///设置图像输出的格式
Response.ContentType = "image/jpeg ";
///保存数据流
bitMapImage.Save(Response.OutputStream, ImageFormat.Jpeg);
///释放占用的资源
graphicImage.Dispose();
bitMapImage.Dispose();
//Session.Remove(RandomCode);
//Session.Remove( "test ");
}
private String GetRandomint()
{
Random random = new Random();
return (random.Next(10000, 99999).ToString());
}
}
以上是获取验证码的,没什么问题。
using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Xml ;
using System.Data.SqlClient;
using System.Drawing;
using DataLayer;
public partial class ujseims : System.Web.UI.Page
{
////////////////////////////////////////////////////////////////////////////////////////////////////
/// <summary>
/// 页面加载
/// </summary>
/// <param name= "sender "> </param>
/// <param name= "e "> </param>
///
protected void Page_Load(object sender, EventArgs e)
{
//设备添加过程中的当前返回值应当添加的行号
if (!IsPostBack)
{
this.SmartNavigation = true;
}
}
////////////////////////////////////////////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////////////////////////////////////////////
/// <summary>
/// 用户名、密码提交
/// </summary>
/// <param name= "sender "> </param>
/// <param name= "e "> </param>
protected void btnConfirm_Click(object sender, ImageClickEventArgs e)
{
if (Session[ "test "] != null && txtValidate.Text.Trim() == Session[ "test "].ToString().Trim() )
{.......}
else// {
//显示错误信息
if (Session[ "test "] == null) Response.Write( " <script language= 'javascript '> alert( ' " + "Session 为 null! " + " '); </script> ");
else
Response.Write( " <script language= 'javascript '> alert( ' " + "验证码错误! " + " '); </script> ");
}
}
就是这里每次运行到if (Session[ "test "] != null && txtValidate.Text.Trim() == Session[ "test "].ToString().Trim() )时Session[ "test "] 就为NULL?
不知道是什么原因?
[解决办法]
txtValidate.Text.Trim() == Session[ "test "].ToString().Trim()
这个值能保证没有问题吗,搞不好是它啊
[解决办法]
客户端禁止了 cookie?
[解决办法]
那可能是复值顺序问题,还有客户端和服务端的区别
<%session( "xx ")= "xx "%>
这里用javascript附值
<%response.write session( "xx ")%>
这样javascript附值是无效的