cookies读写删

<%@ language="VB"%>
<%@ import namespace="System" %>
<%@ import namespace="System.Web" %>
<%@ import namespace="System.Web.HttpCookie" %>
<HTML>
<title>Cookies的写入和读取</title>
 <script language="VB" runat="server">

  Sub btn1(sender as object,e as eventargs)
'判断是否浏览者选取了允许保存Cookies的选项和输入的账号和密码是否为空
   if Checkbox1.checked and Not(name.text ="") and Not(ps.text ="") then
'向Cookies中写入浏览者输入的账号和密码
   dim cookie as HttpCookie
   cookie=new HttpCookie("mychoices")
          cookie.Values.Add("name",name.text)
          cookie.Values.Add("ps",ps.text)
   Dim dt As DateTime = DateTime.Now
'加入当前时间
   cookie.Values.Add("time",dt.tostring)
   Dim ds As timespan
   ds = new timespan(0,0,10,2)
'保存入Cookies   
   cookie.Expires=dt.add(ds)
   response.appendcookie(cookie)
   
   label1.text="<font color=red size=5>成功向Cookies写入</font>"
  
 Else
  label1.text="<font color=red size=5>没有存入任何内容到Cookies中</font>"
 End If
'为了保证安全性,清空用户的输入
  name.text=""
 ps.text=""
 End Sub

 Sub btn2(sender as object,e as eventargs)
'判断是否存在该Cookies
 If request.cookies("mychoices") Is Nothing then
  label1.text="<font color=red size=5>没有这个Cookies</font>"
 Else
  
  dim cookie as HttpCookie
  cookie=new HttpCookie("mychoices")
'如果以前保存的用户名和账号不为空则读出账号和密码
   If Not(request.cookies("mychoices").values("name") ="") And Not(request.cookies("mychoices").values("ps") = "")
  label3.text ="保存在cookies中的用户名:" & "<font color=red size=5>" & request.cookies("mychoices").values("name") & "</font>"
  label4.text ="保存在cookies中的密 码 :" & "<font color=red size=5>" & request.cookies("mychoices").values("ps") & "</font>"
  label1.text="<font color=red size=5>成功读出Cookies中内容</font>"

   Else
'否则显示错误信息
   label1.text="<font color=red size=5>Cookies中没有任何内容</font>"
   End If
 End If

 End Sub

 Sub btn3(sender as object,e as eventargs)
'判断是否存在该Cookies
 If request.cookies("mychoices") Is Nothing then
  label1.text="<font color=red size=5>没有这个Cookies</font>"
 Else
'如果以前保存的用户名和账号不为空,则清除账号和密码
  If Not(request.cookies("mychoices").values("name") ="") And Not(request.cookies("mychoices").values("ps") = "")
  dim cookie as HttpCookie
  cookie=new HttpCookie("mychoices")
         cookie.Values.Add("name","")
         cookie.Values.Add("ps","")
  label3.text =""
  label4.text =""
  Dim dt As DateTime = DateTime.Now
'加入当前时间
   cookie.Values.Add("time",dt.tostring)
   Dim ds As timespan
   ds = new timespan(0,0,10,2)
'保存入Cookies   
   cookie.Expires=dt.add(ds)
  response.appendcookie(cookie)
  label1.text="<font color=red size=5>成功清除Cookies中内容</font>"
  Else
'否则显示错误信息
  label1.text="<font color=red size=5>Cookies中没有任何内容可供清除</font>"
  End If
 End If

 End Sub


 </script>
 <body>
    <h3><font face="Verdana">Cookies的写入和读取</font></h3><br><Br>
  <Form runat="server">
   <P>
    输入您的用户名:<asp:TextBox id="name" Columns="25" MaxLength="30" runat="server" />
    <br>
    输入您的密 码 :<asp:TextBox id="ps" Columns="25" MaxLength="30" runat="server" TextMode="Password" />
    <asp:button id="button1" OnClick="btn1" text=" 登录" runat="server" />
   </P>
   <P>
    &nbsp;
    <asp:CheckBox id="CheckBox1" runat="server" Checked="True"></asp:CheckBox>
    <asp:Label id="Label2" runat="server">保存账号密码到Cookies</asp:Label>
   </P>
   <P>
    <FONT face="宋体">&nbsp;</FONT>
    <asp:label id="label3" text="" runat="server" />
    <BR>
    <FONT face="宋体">&nbsp;</FONT>
    <asp:label id="label4" text="" runat="server" />
   </P>
   <p>
    <asp:button id="button2" OnClick="btn2" text="读出cookies" runat="server" />
    <asp:button id="button3" OnClick="btn3" text="清除cookies" runat="server" />
    <BR>
   <p>
    <asp:label id="label1" text="" runat="server" />
  </Form>
  </P>
 </body>
</HTML>

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值