<OPTION>Visit_Count</OPTION>
<OPTION>My_New_Value</OPTION>
</SELECT>
(1) 删除单个值
当单击按钮删除单个值时,该窗体再次提交给相同的网页,但是这一次将查找名为cmdRemoveThis的SUBMIT按钮。如果存在(即单击了这个按钮),则使用列表框的值,调用Application.Contents集合的Remove方法:
If Len(Request.Form("cmdRemoveThis")) Then
strToRemove = Request.Form("lstRemove")
Response.Write "strToRemove = " & strToRemove
Application.Lock
Application.Contents.Remove(strToRemove)
Application.Unlock
End If
注意这是Contents集合的一个方法,而不是Application对象的。语法是Application.Contents.Remove,而不是
Application.Remove。
从Contents集合中删除Start_Time值的结果如图3-18所示:
图3-18 删除Start_Time值后的屏幕
(2) 删除所有的值
如果单击三个SUBMIT类型按钮中的最后一个(如图3-18所示),该网页中的代码将检测到单击的按钮为cmdRemoveAll,将
执行Application.Contents集合的RemoveAll方法:
If Len(Request.Form("cmdRemoveAll")) Then
Application.Lock
Application.Contents.RemoveAll
Application.Unlock
End If
再次提醒,这是Contents集合的一个方法,而不是Application。语法是Application.Contents.RemoveAll,而不是
Application.RemoveAll。
图3-19所示的是从Contents集合中删除所有值的结果(记住在运行时间不能从StaticObjects集合删除项):
图3-19 删除Contents集合中所有值的屏幕
3.3.5 活动中的ASP Session对象
示例网页的第二个示例页面show_session.asp,示范了如何使用Session对象。可在Chapter03子目录中的开始菜单(Default.asp)中打开它。
1. 显示和更新Session集合
Session对象示例页面看起来与刚刚使用过的Application对象示例页面相似。它遍历Session对象的Contents和StaticObjects集合,显示其名字和(可能的话)相应的值。如果把这些值与Application对象页面进行比较,将会看到不同之处。
这里还能够看到客户端IP地址的一些其他值。这是当会话启动时global.asa中的代码从Request.ServerVariables集合中得到的。这个页面还显示四个会话属性的值,如图3-20所示:
图3-20 Session对象显示属性的屏幕
下面是例子中使用的golbal.asa文件的相关段落,它把缺省值增加到图3-20所示的屏幕上所看到的会话中:
...
<!-- Declare instance of the ASPContentLink component
with session-level scope //-->
<OBJECT ID="ASPContentLink" RUNAT="Server" SCOPE="Session"
PROGID="MSWC.NextLink">
</OBJECT>
<SCRIPT LANGUAGE="VBScript" RUNAT="Server">
...
...
Sub Session_onStart()
'Create an instance of the AdRotator component with session-level scope
Set Session("ASPAdRotator") = Server.CreateObject("MSWC.AdRotator")
Dim varArray(3) 'Create a Variant array and fill it
varArray(0) = "This is a"
varArray(1) = "Variant array"
varArray(2) = "stored in the"
varArray(3) = "Session object"
Session("Variant_Array") = varArray 'Store it in the Session
Session("Start_Time") = CStr(Now) 'Store the date/time as a string
'We can access the contents of the Request and Response in a Session_onStart
'event handler for the page that initiated the session. This is the *only*
'place that the ASP page context is available like this.
'as an example, we can get the IP address of the user:
Session("Your_IP_Address") = Request.ServerVariables("REMOTE_ADDR")
Application.Lock 'Prevent concurrent updates
intVisits = Application("Visit_Count") 1 'Increment counter variable
Application("Visit_Count") = intVisits 'Store back in Application
Application.Unlock 'Release lock on Application
End Sub
...
...
</SCRIPT>
遍历Contents和StaticObjects集合的代码与前面在Application对象示例中使用的代码一样,只不过这里引用了Session.Contents和Session.StaticObjects集合,而不是Application.Contents和Appliction.StaticObjects集合。
靠近页面底部的按钮的功能是把值增加到Session.Contents集合和从Session.Contents集合删除值。这些按钮与在Application对象示例页面中相应的按钮工作方式相同,这里访问的是Session.Contents集合,以及相应的Remove和
RemoveAll方法。我们不再重复解释。
2. 终止一个用户会话
在Session对象页面的底部有一个按钮,该按钮终止当前的用户会话,这通过调用Session对象的Abandon方法实现。它与其余的HTML控件在相同的窗体上,名为cmdAbandon。当该窗体再次被提交给这个网页时,在Request.Form集合中查找这个值(如同在Application对象例子中做的一样)。如发现这个值,则将该用户重定向到另一个网页:
If Len(Request.Form("cmdAbandon")) Then
Response.Clear
Response.Redirect "abandon.asp"
Response.End
End If
新的页面名为abandon.asp,十分简单,除了创建消息的文本和HTML以外只有如下的代码:
<% Session.Abandon %>
该网页的其余部分只是一个包含单个SUBMIT按钮的窗体。注意如何使用来自Request.ServerVariables集合的引用网页
文章整理:西部数码--专业提供域名注册、虚拟主机服务
http://www.west263.com
以上信息与文章正文是不可分割的一部分,如果您要转载本文章,请保留以上信息,谢谢!




