我在做练习的时候遇到了这样一个问题,在母版页页面中写入登录和密码修改的js代码,在登录的方法中写入 HttpContext.Current.Session.Add("UserPwd", dtUser.Rows[0][1].ToString()),以便让这个session在做密码修改的时候和用户输入的密码做比较。但是在母版页的js代码中写入Session["UserPwd"].ToString()总是报错,错误提示是:未将引用对象实例化。在母版页的page_load()方法中写入string ss=Session["UserPwd"].ToString()是可以得到密码的。 在同事的帮助下,找到了解决方法,就是在js方法中将Session["UserPwd"].ToString()改成HttpContext.Current.Session["UserPwd"].ToString()就可以得到密码了。但此时page_load()方法下的string ss=Session["UserPwd"].ToString()又开始报错。所以新的问题又出来了,Session与HttpContext.Current.Session到底有什么区别呢?以下是我在MSDN中找到的结果: Session 属性提供对 HttpSessionState 类的属性和方法的编程访问。由于 ASP.NET 页包含对 System.Web 命名空间(含有 HttpContext 类)的默认引用,因此在 .aspx 页上可以引用 HttpContext 的成员,而不需要使用对 HttpContext 的完全限定类引用。例如,可使用 Session("SessionVariable1") 获取或设置会话状态变量 SessionVariable1 的值。但是,如果要从 ASP.NET 代码隐藏模块中使用 HttpResponse 的成员,则必须在模块中包括对 System.Web 命名空间的引用,同时还要包括对当前活动的请求/响应上下文以及要使用的 System.Web 中的类的完全限定引用。例如,在代码隐藏页中,必须指定完全限定名称 HttpContext.Current.Session("SessionVariable1")。 如果未启用会话状态,则无法设置或获取会话状态值。若要为应用程序配置会话状态设置,请在 Web.config 文件中设置 sessionState 元素的 mode 属性。启用会话状态后,如果请求一个会话状态变量中不存在的值,则会返回 null。 难道是和这个有关系么?如果说二者没有区别,为什么在将Session["UserPwd"].ToString()改成HttpContext.Current.Session["UserPwd"].ToString()之后,原本正确可以得到结果的string ss=Session["UserPwd"].ToString()报错了呢?还希望在这个平台上得到大家的帮助!谢谢
解决方案 »
- 我们平时在网页、word文档、文本文件中编辑的中文汉字是否也是一种叫什么编码?
- 用webbrowser控件,抓取网页数据,如何抓取多个a标签对应的url地址的网页数据
- 为什么不行
- DevExpress.XtraReports.UI.XtraReport xrTable绑定数据问题
- directshow做的播放器
- 在propertygrid控件中怎么不显示属性的注释?
- 关于一个水晶报表的棘手问题
- 液晶出了坏点,JS说非要三个以上才能换,有法用程序在屏幕上自己画个点不?
- 哪们高手知道哪里有左右分栏的论坛系统啊???
- 请问如何让一个窗体只有一个实例可以显示?
- 根据两点,获取贝塞尔曲线.
- 求数据备份与还原的实现方法
HttpContext指的是当前请求的HttpHandler实例,而Page就是这个实例,所以访问上没有区别,主要用于一些场景无法获取Page对象,只能用上下文来获取请求实例的情况
看了你的回复。我的问题又出来了,“根据网友的介绍的HttpContext.Current.Session
这个通常用在单独的类里。Page.Session一般是页面的后置类里用。”这是摘自你提供网页上的一段话,请问什么叫后置类?
这是用reflector查看到的代码。protected internal override HttpContext Context
{
[TargetedPatchingOptOut("Performance critical to inline across NGen image boundaries")]
get
{
if (this._context == null)
{
this._context = HttpContext.Current;
}
return this._context;
}
}
public virtual HttpSessionState Session
{
get
{
if (!this._sessionRetrieved)
{
this._sessionRetrieved = true;
try
{
this._session = this.Context.Session;
}
catch
{
}
}
if (this._session == null)
{
throw new HttpException(SR.GetString("Session_not_enabled"));
}
return this._session;
}
}
public partial class MasterPageApplication_ParentPage : System.Web.UI.MasterPage
{
public string strCon = System.Configuration.ConfigurationManager.AppSettings["ConnStr"]; protected void Page_Load(object sender, EventArgs e)
{
Ajax.Utility.RegisterTypeForAjax(typeof(MasterPageApplication_ParentPage));
incon.Value = this.Application["ConnStr"].ToString(); if (!IsPostBack)
{
#region 初始化
if (Session["LoginUserNm"] != null)
{
UserName.Text = Session["LoginUserNm"].ToString();
divuserid.Style["display"] = "none";
divpassword.Style["display"] = "none";
divusername.Style["display"] = "";
btnlogin.Style["display"] = "none";
btnlogout.Style["display"] = "";
btnmodify.Style["display"] = "";
}
else
{
divuserid.Style["display"] = "";
divpassword.Style["display"] = "";
divusername.Style["display"] = "none";
btnlogin.Style["display"] = "";
btnlogout.Style["display"] = "none";
btnmodify.Style["display"] = "none";
}
#endregion
}
}
}
public class PubLib : System.Web.UI.Page, IRequiresSessionState
注意,在你的类后面加"IRequiresSessionState"就可以与PAGE中的使用同一个SESSION值