验证码写入session以后,其他页面如何获取啊?

phuai007 2011-01-15 11:26:01
生成验证码页面:yanzhen.aspx.cs


ystem.Random a = new Random(System.DateTime.Now.Millisecond);
Int32 A = a.Next(10, 30);
Int32 B = a.Next(1, 20);
String jieguo = (A + B).ToString();
//写入Session
Session["CheckCode"] = jieguo;



需要取得session值的页面怎么取这个啊,我用 String yanzhen = HttpContext.Current.Session["CheckCode"].ToString(); 运行的时候提示错误:
用户代码未处理 System.NullReferenceException
Message="未将对象引用设置到对象的实例。"
Source="web2"
StackTrace:
在 web2.userreg..ctor() 位置 D:\web2\web2\userreg.aspx.cs:行号 26
在 ASP.userreg_aspx..ctor() 位置 c:\WINDOWS\Microsoft.NET\Framework\v2.0.50727\Temporary ASP.NET Files\root\21c64b4e\b657b993\App_Web_cbhpdmny.1.cs:行号 0
在 __ASP.FastObjectFactory_app_web_cbhpdmny.Create_ASP_userreg_aspx() 位置 c:\WINDOWS\Microsoft.NET\Framework\v2.0.50727\Temporary ASP.NET Files\root\21c64b4e\b657b993\App_Web_cbhpdmny.3.cs:行号 0
在 System.Web.Compilation.BuildResultCompiledType.CreateInstance()
在 System.Web.Compilation.BuildManager.CreateInstanceFromVirtualPath(VirtualPath virtualPath, Type requiredBaseType, HttpContext context, Boolean allowCrossApp, Boolean noAssert)
在 System.Web.UI.PageHandlerFactory.GetHandlerHelper(HttpContext context, String requestType, VirtualPath virtualPath, String physicalPath)
在 System.Web.UI.PageHandlerFactory.System.Web.IHttpHandlerFactory2.GetHandler(HttpContext context, String requestType, VirtualPath virtualPath, String physicalPath)
在 System.Web.HttpApplication.MapHttpHandler(HttpContext context, String requestType, VirtualPath path, String pathTranslated, Boolean useAppConfig)
在 System.Web.HttpApplication.MapHandlerExecutionStep.System.Web.HttpApplication.IExecutionStep.Execute()
在 System.Web.HttpApplication.ExecuteStep(IExecutionStep step, Boolean& completedSynchronously)
InnerException:

...全文
231 10 打赏 收藏 转发到动态 举报
写回复
用AI写文章
10 条回复
切换为时间正序
请发表友善的回复…
发表回复
  • 打赏
  • 举报
回复
if (HttpContext.Current.Session["CheckCode"] != null)
{
yanzhen = HttpContext.Current.Session["CheckCode"].ToString();
}
wuyq11 2011-01-15
  • 打赏
  • 举报
回复
public partial class CheckCode : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
Session["code"] = ValidCode().MakeValidateCode();
}
}
protected void btnSubmit_Click(object sender, EventArgs e)
{
if (Page.IsValid)
{
if (Session["code"] == null)
{
return;
}
if (Session["code"].ToString() == this.txtcode.Text.Trim())
{}
}
}
ycproc 2011-01-15
  • 打赏
  • 举报
回复
HttpContext.Current.Session["sessionname"]
phuai007 2011-01-15
  • 打赏
  • 举报
回复

protected void Page_Load(object sender, EventArgs e)
{
string yanzhen = string.Empty;
if (HttpContext.Current.Session["CheckCode"] != null)
{
yanzhen = HttpContext.Current.Session["CheckCode"].ToString();
}
else
{
yanzhen = "Session是空的";
}

Label6.Text = yanzhen;
}


基本上解决了获取的问题,但是新问题又出来了,因为第一次启动页面的时候,因为验证码那边还没有写入session,所以这次获取到的是空,但是页面刷新以后,session获得了上一次的值,但是页面上的验证码又变化了....
telankes2000 2011-01-15
  • 打赏
  • 举报
回复
Session 是全局的
在調用的頁面 直接取值即可
if(Session["code"] != null){
//
}
xiaolingwei 2011-01-15
  • 打赏
  • 举报
回复
if (HttpContext.Current.Session["CheckCode"]!= null)
{
string reName=HttpContext.Current.Session["CheckCode"].ToString();
}


kira155716 2011-01-15
  • 打赏
  • 举报
回复
你是在什么地方引用SESSION的?

[Quote=引用 2 楼 x895238 的回复:]
string Message=Session["CheckCode"].Tostring();
[/Quote]
是没错的,如果是在普通页面的话
phuai007 2011-01-15
  • 打赏
  • 举报
回复
[Quote=引用 2 楼 x895238 的回复:]
string Message=Session["CheckCode"].Tostring();
[/Quote]

这样更不行了
错误 非静态的字段、方法或属性“System.Web.UI.Page.Session.get”要求对象引用
也不知道要引用哪个
x895238 2011-01-15
  • 打赏
  • 举报
回复
string Message=Session["CheckCode"].Tostring();
  • 打赏
  • 举报
回复
在本页面实例化一个Session

62,266

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术交流专区
javascript云原生 企业社区
社区管理员
  • ASP.NET
  • .Net开发者社区
  • R小R
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

.NET 社区是一个围绕开源 .NET 的开放、热情、创新、包容的技术社区。社区致力于为广大 .NET 爱好者提供一个良好的知识共享、协同互助的 .NET 技术交流环境。我们尊重不同意见,支持健康理性的辩论和互动,反对歧视和攻击。

希望和大家一起共同营造一个活跃、友好的社区氛围。

试试用AI创作助手写篇文章吧