CSDN首页 空间 新闻 论坛 Blog 下载 读书 网摘 搜索 .NET Java 视频 接项目 求职 在线学习 买书 程序员 通知
山寨机中的战斗机! 程序优化工程师到底对IT界有没有贡献
CSDN社区
搜索 收藏 打印 关闭
CSDN社区 >  .NET技术 >  ASP.NET

关于cookie的一个简单问题

楼主jonathonc(bachelor)2005-08-02 08:14:37 在 .NET技术 / ASP.NET 提问

我想单击按钮后查询数据库中有无TextBox1.Text所指用户,如有则生成Cookie,通过判断cookie有无改变页面。但我输入数据库中已有用户后,第一次单击按钮页面无变化,第二次才发生变化,为何?  
        Private   Sub   Page_Load(ByVal   sender   As   System.Object,   ByVal   e   As   System.EventArgs)   Handles   MyBase.Load  
                  If   Not   Request.Cookies("EmailRegister")   Is   Nothing   Then  
                          TextBox1.Visible   =   False  
                          Button1.Visible   =   False  
                          Label2.Visible   =   True  
                          Label2.Text   =   "You   have   successfully   registered   for   Email   updates"  
                  End   If  
          End   Sub  
   
          Private   Sub   Button1_Click(ByVal   sender   As   System.Object,   ByVal   e   As   System.EventArgs)   Handles   Button1.Click  
                  Dim   lgConnection   As   New   OleDbConnection("Provider=Microsoft.Jet.oledb.4.0;data   source=C:\Inetpub\wwwroot\lg.mdb")  
                  Dim   Selecting   As   New   OleDbCommand("select   count   (username)   from   accounts   where   username=@User",   lgConnection)  
                  Dim   Param   As   New   OleDbParameter("@User",   OleDbType.VarChar,   20)  
                  Param.Value   =   TextBox1.Text  
                  Selecting.Parameters.Add(Param)  
                  Dim   Result   As   Integer  
                  lgConnection.Open()  
                  Result   =   Selecting.ExecuteScalar  
                  lgConnection.Close()  
                   
            If   result>0   then  
                  Dim   Cookie   As   New   HttpCookie("EmailRegister")  
                  Cookie.Value   =   TextBox1.Text  
                  Cookie.Expires   =   Now.AddSeconds(10)  
                  Response.Cookies.Add(Cookie)  
            End   If  
          End   Sub  
  问题点数:30、回复次数:3Top

1 楼bingbingcha(不思不归,不孟不E,原来是头大灰狼)回复于 2005-08-02 08:43:04 得分 10

当然会有这样的结果了..你的按钮在点击后去判断数据库..然后存在的时候才赋予Cookie值..判断后并没有赋予相应的处理,所以第一点击的时候会赋予Cookie值..第二次点击的时候你的判断Cookie才会在Page_Load中起作用.  
   
   
  Private   Sub   Button1_Click(ByVal   sender   As   System.Object,   ByVal   e   As   System.EventArgs)   Handles   Button1.Click  
                  Dim   lgConnection   As   New   OleDbConnection("Provider=Microsoft.Jet.oledb.4.0;data   source=C:\Inetpub\wwwroot\lg.mdb")  
                  Dim   Selecting   As   New   OleDbCommand("select   count   (username)   from   accounts   where   username=@User",   lgConnection)  
                  Dim   Param   As   New   OleDbParameter("@User",   OleDbType.VarChar,   20)  
                  Param.Value   =   TextBox1.Text  
                  Selecting.Parameters.Add(Param)  
                  Dim   Result   As   Integer  
                  lgConnection.Open()  
                  Result   =   Selecting.ExecuteScalar  
                  lgConnection.Close()  
                   
            If   result>0   then  
                  Dim   Cookie   As   New   HttpCookie("EmailRegister")  
                  Cookie.Value   =   TextBox1.Text  
                  Cookie.Expires   =   Now.AddSeconds(10)  
                  Response.Cookies.Add(Cookie)  
                  TextBox1.Visible   =   False  
                  Button1.Visible   =   False  
                  Label2.Visible   =   True  
                  Label2.Text   =   "You   have   successfully   registered   for   Email   updates"  
   
            End   If  
          End   Sub  
   
  Top

2 楼wu896222(WYF)回复于 2005-08-02 08:48:53 得分 10

页面事件触发的过程是:   先Page_Load,再Button1_Click.   所以第一次按钮点击,Page_Load中未找到Cookie,第二次点击才找到Cookie.Top

3 楼jimu8130(火箭的未来在哪里?)回复于 2005-08-02 10:10:23 得分 10

上面的几位都说明原因了  
  最好你能在buttonclick里面判断后就得有相应的动作Top

相关问题

  • cookie简单问题
  • 急!!!!!!!!一个关于Cookie的简单问题!!!!!
  • 简单的问题,关于读写cookie!!!!!在javascript中,
  • 简单!
  • 简单
  • 简单~~~
  • 简单
  • 简单!
  • 简单
  • 简单

关键词

  • 数据库
  • lgconnection
  • emailregister
  • cookie
  • dim
  • 判断
  • 按钮
  • 赋予
  • textbox
  • byval

得分解答快速导航

  • 帖主:jonathonc
  • bingbingcha
  • wu896222
  • jimu8130

相关链接

  • CSDN .NET频道
  • .NET类图书
  • C#类图书
  • .NET类源码下载

广告也精彩

反馈

请通过下述方式给我们反馈
反馈
提问
网站简介|广告服务|VIP资费标准|银行汇款帐号|网站地图|帮助|联系方式|诚聘英才|English|问题报告
北京创新乐知广告有限公司 版权所有, 京 ICP 证 070598 号
世纪乐知(北京)网络技术有限公司 提供技术支持
Copyright © 2000-2008, CSDN.NET, All Rights Reserved
GongshangLogo