帝国软件
  设为首页 加入收藏 关于我们
 
解密帝国网站管理系统
栏 目:
 
您的位置:首页 > 技术文档 > ASP编程
在ASP+中使用Cookie
作者: 发布时间:2005-03-12 来源:
<%@ 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+ PDC bits的经验  (2005-03-12)
 ·在ASP中使用Java类(Using Java C  (2005-03-12)
 ·在ASP中改善动态分页的性能  (2005-03-12)
 ·在asp中调用jsp  (2005-03-12)
 ·较长text型数据无法在Asp页面中取  (2005-03-12)
 ·在ASP中用“正则表达式对象”来校  (2005-03-12)
 ·在ASP中列出数据库中的表名和字段  (2005-03-12)
 ·在ASP中如何将代码生成的文件设为  (2005-03-12)
 ·在ASP里建表  (2005-03-12)
 ·怎样在ASP里面创建统计图表  (2005-03-12)

   栏目导行
  PHP编程
  ASP编程
  ASP.NET编程
  JAVA编程
   站点最新
·致合作伙伴的欢迎信
·媒体报道
·帝国软件合作伙伴计划协议
·DiscuzX2.5会员整合通行证发布
·帝国CMS 7.0版本功能建议收集
·帝国网站管理系统2012年授权购买说
·PHPWind8.7会员整合通行证发布
·[官方插件]帝国CMS-访问统计插件
·[官方插件]帝国CMS-sitemap插件
·[官方插件]帝国CMS内容页评论AJAX分
   类别最新
·在ASP中使用数据库
·使用ASP脚本技术
·通过启动脚本来感受ASP的力量
·学习使用ASP对象和组件
·解析asp的脚本语言
·初看ASP-针对初学者
·ASP开发10条经验总结
·ASP之对象总结
·ASP与数据库应用(给初学者)
·关于学习ASP和编程的28个观点
 
关于帝国 | 广告服务 | 联系我们 | 程序开发 | 网站地图 | 留言板 帝国网站管理系统