论坛交流
首页办公自动化| 网页制作| 平面设计| 动画制作| 数据库开发| 程序设计| 全部视频教程
应用视频: Windows | Word2007 | Excel2007 | PowerPoint2007 | Dreamweaver 8 | Fireworks 8 | Flash 8 | Photoshop cs | CorelDraw 12
编程视频: C语言视频教程 | HTML | Div+Css布局 | Javascript | Access数据库 | Asp | Sql Server数据库Asp.net  | Flash AS
当前位置 > 文字教程 > asp教程
Tag:入门,文摘,实例,技巧,iis,表单,对象,上传,数据库,记录集,session,cookies,存储过程,注入,分页,安全,优化,xmlhttp,fso,jmail,application,防盗链,stream,组件,md5,乱码,缓存,加密,验证码,算法,ubb,正则表达式,水印,,日志,压缩,url重写,控件,函数,破解,触发器,socket,ADO,初学,聊天室,留言本,视频教程

在ASP+中使用Cookie

文章类别:asp | 发表日期:2008-10-5 20:53:04

<%@ Page Language="VB" %>

<script language="VB" runat="server">
Const COOKIE_NAME  As String = "test-cookie-name"
Const COOKIE_VALUE As String = "test-cookie-value"

' Declare our cookie object
Dim objCookieObject As HttpCookie

Sub btnSetCookie_OnClick(Sender As Object, E As EventArgs)
' Create a cookie object - I'm passing name and value,
' but you can also pass in a name and set the value later.
' ie. objCookieObject = New HttpCookie(COOKIE_NAME)
objCookieObject = New HttpCookie(COOKIE_NAME, COOKIE_VALUE)

' We already set these above!
'objCookieObject.Name   = COOKIE_NAME
'objCookieObject.Value  = COOKIE_VALUE

' Additional cookie properties:
objCookieObject.Expires = New DateTime(2001, 12, 31, 23, 59, 59)

' Normally you can leave these alone.
' The defaults will work fine for most uses.
'objCookieObject.Domain  = "www.domain.com"
'objCookieObject.Path    = "/path/"
'objCookieObject.Secure  = True

    Response.AppendCookie(objCookieObject)
End Sub

Sub btnRemoveCookie_OnClick(Sender As Object, E As EventArgs)
objCookieObject = New HttpCookie(COOKIE_NAME)

' Expire it on the day I was born just so we're sure it's a date in the past.
objCookieObject.Expires = New DateTime(1974, 11, 12)

Response.AppendCookie(objCookieObject)
End Sub

Sub btnGetCookie_OnClick(Sender As Object, E As EventArgs)
objCookieObject = Request.Cookies(COOKIE_NAME)

If Not(objCookieObject = null) Then
lblCookieDetails.Text        = objCookieObject.Name

lblCookieDetailsName.Text    = objCookieObject.Name
lblCookieDetailsValue.Text   = objCookieObject.Value
lblCookieDetailsExpires.Text = objCookieObject.Expires.ToString
lblCookieDetailsDomain.Text  = objCookieObject.Domain
lblCookieDetailsPath.Text    = objCookieObject.Path
lblCookieDetailsSecure.Text  = objCookieObject.Secure.ToString
lblCookieDetailsHasKeys.Text = objCookieObject.HasKeys.ToString
Else
lblCookieDetails.Text        = "Cookie Not Set!"

lblCookieDetailsName.Text    = ""
lblCookieDetailsValue.Text   = ""
lblCookieDetailsExpires.Text = ""
lblCookieDetailsDomain.Text  = ""
lblCookieDetailsPath.Text    = ""
lblCookieDetailsSecure.Text  = ""
lblCookieDetailsHasKeys.Text = ""
End If

' I'm ignoring collections.  They're outside the realm of this basic sample.
' FYI: Additional properties related to cookie collections: Values, Item
End Sub
</script>

<html>
<body>

<h4>The cookie name we're using for this sample is: <em><%= COOKIE_NAME %></em></h4>

<form action="cookies.aspx" method="post" runat="server">
<asp:Button type="submit" id="btnSetCookie" text="Set Cookie" OnClick="btnSetCookie_OnClick"
runat="server" />
<asp:Button type="submit" id="btnRemoveCookie" text="Remove Cookie"
OnClick="btnRemoveCookie_OnClick" runat="server" />

<p>
To see the cookie's current status you'll need to click below.  This is because the response
which adds or deletes the cookie happens after the request is already done.  As such, those changes aren't
available from the request collection until the next request.
</p>

<asp:Button type="submit" id="btnGetCookie" text="Get Cookie Details"
OnClick="btnGetCookie_OnClick" runat="server" />
</form>

<p>
<strong>Details of:</strong> <asp:label id="lblCookieDetails" runat="server" />
</p>

<table border="1">
<thead>
<tr>
<th>Property</th>
<th>Value</th>
</tr>
</thead>
<tbody>
<tr>
<td>Name</td>
<td><asp:label id="lblCookieDetailsName" runat="server" /></td>
</tr>
<tr>
<td>Value</td>
<td><asp:label id="lblCookieDetailsValue" runat="server" /></td>
</tr>
<tr>
<td>Expires</td>
<td><asp:label id="lblCookieDetailsExpires" runat="server" /></td>
</tr>
<tr>
<td>Domain</td>
<td><asp:label id="lblCookieDetailsDomain" runat="server" /></td>
</tr>
<tr>
<td>Path</td>
<td><asp:label id="lblCookieDetailsPath" runat="server" /></td>
</tr>
<tr>
<td>Secure</td>
<td><asp:label id="lblCookieDetailsSecure" runat="server" /></td>
</tr>
<tr>
<td>Has Keys</td>
<td><asp:label id="lblCookieDetailsHasKeys" runat="server" /></td>
</tr>
</tbody>
</table>

</body>
</html>


上一篇:{技巧}asp+ 如何跨站抓取页面 人气:3282
下一篇:{技巧}如何用asp进行base64加密 人气:3431
视频教程列表
文章教程搜索
 
Asp推荐教程
Asp热门教程
看全部视频教程
购买方式/价格
购买视频教程: 咨询客服
tel:15972130058