⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 asp05.htm

📁 VB.NET网络编程指南 书籍语言: 简体中文 书籍类型: 程序设计 授权方式: 免费软件 书籍大小: 1.19 MB
💻 HTM
📖 第 1 页 / 共 2 页
字号:
  5.6 Application对象<br>
  Application对象的用途是记录整个网站的信息,语法:<br>
  Application(&quot;变量&quot;) = &quot;变量内容&quot;<br>
  Application(&quot;对象名&quot;) = Server.CreateObject(ProgId) <br>
  表分别列出了Application对象的常用属性和方法。</p>
<p align="center"> <img src="image/slide5image014.PNG" width="625" height="74"><br>
  <img src="image/slide5image015.PNG" width="623" height="101"><br>
  <img src="image/slide5image016.PNG" width="623" height="288"></p>
<p>5.6.1 存取Application对象变量值<br>
  语法:<br>
  变量 = Application(&quot;变量名称&quot;)<br>
  Application(“变量名称”) = 表达式 <br>
  【例】向Application对象中添加三个变量并赋值,三个变量的值分别为“Value1”、“Value2” 和“Value3”。通过循环显示这三个Application变量的名字和值,显示完以后清除。 
  <br>
  &lt;SCRIPT Language=&quot;VB&quot; Runat=&quot;Server&quot;&gt; <br>
  Sub Page_Load(Sender As Object,e As Eventargs) <br>
  &nbsp; Dim shtI As Short<br>
  &nbsp; Application.Add(&quot;App1&quot;,&quot;Value1&quot;)'向Application对象中添加三个变量并赋值<br>
  &nbsp; Application.Add(&quot;App2&quot;,&quot;Value2&quot;) <br>
  &nbsp; Application.Add(&quot;App3&quot;,&quot;Value3&quot;) <br>
  &nbsp; For shtI=0 To Application.Count-1 '显示三个Application变量名和值 <br>
  &nbsp;&nbsp; Response.Write(&quot;变量名:&quot; &amp; Application.GetKey(shtI) 
  ) <br>
  &nbsp;&nbsp; Response.Write(&quot; ,变量值:&quot; &amp; Application.Item(shtI) 
  &amp; &quot;&lt;P&gt;&quot;) <br>
  &nbsp; Next<br>
  &nbsp; Application.Clear() '清除Application对象变量<br>
  End Sub <br>
  &lt;/SCRIPT&gt;<br>
  &nbsp;&nbsp;&nbsp; 上述程序利用Application的Add()方法,向Application对象中添加三个值分别为&quot;Value1&quot;、 
  “Value2“和”Value3”的变量。程序运行结果如图所示。 Application变量的存取 5.6.2 锁定Application对象 语法:<br>
  Application.Lock <br>
  Application(&quot;变量&quot;) = 表达式 <br>
  Application.UnLock <br>
  【例】一个简单计数器程序,在 ASP.NET网页每执行一次,Counter变量 值就加1,在累计之前,先锁定Application 对象,让其他人无法使用此对象,待累计完成后再解除所定。<br>
  &lt;SCRIPT Language=&quot;VB&quot; Runat=&quot;Server&quot;&gt; <br>
  Sub Page_Load(sender as Object, e as Eventargs) <br>
  &nbsp; Application.Lock()'锁定,不允许其他用户修改变量 <br>
  &nbsp; Application.Set(&quot;Counter&quot;, Application(&quot;Counter&quot;) 
  + 1)'访问计数增加1 <br>
  &nbsp; Application.UnLock()'开锁,允许其他用户修改变量 <br>
  &nbsp; myCounter.Text = &quot;您是第 &quot; &amp; Application(&quot;Counter&quot;) 
  &amp; &quot; 位访客&quot; <br>
  End Sub <br>
  &lt;/SCRIPT&gt; <br>
  &lt;HTML&gt;<br>
  &lt;BODY&gt;<br>
  &lt;Asp:Label Runat=&quot;Server&quot; Id=&quot;myCounter&quot; /&gt; <br>
  &lt;/BODY&gt;<br>
  &lt;/HTML&gt; <br>
  5.6.3 Application对象的事件 Application对象有以下四个事件:<br>
  &nbsp; (1) OnStart事件:在整个ASP.Net应用首先被触发的事件,也就是在一个虚拟目录中第一个ASP.Net程序执行时触发。 <br>
  &nbsp; (2) OnEnd事件:在整个应用停止时被触发(通常发生在服务器被重启/关机时)。<br>
  &nbsp; (3) OnBeginRequest事件:在每一个ASP.Net程序被请求时就发生,即客户每访问一个ASP.Net程序时,就触发一次该事件。<br>
  &nbsp; (4) OnEndRequest事件:ASP.NET程序结束时,触发该事件。以下是一个Global.asax文件的示例。<br>
  &lt;SCRIPT Language=&quot;VB&quot; Runat=&quot;Server&quot;&gt; <br>
  Sub Application_OnStart(sender As Object, e As Eventargs) <br>
  &nbsp; Application.Add(&quot;count&quot;,0) <br>
  End Sub <br>
  Sub Application_OnEnd(sender As Object, e As Eventargs)<br>
  &nbsp; Application.RemoveAll()<br>
  End Sub<br>
  &lt;/SCRIPT&gt;<br>
  5.7 Session对象<br>
  Session对象的功能和Application对象一样,都是用来记录浏览器端的变量 。Session对象类名称是HttpSessionState,它和Application对象一样是属于Page对象的成员,所以可以直接使用。 
  语法: <br>
  &nbsp; Session(&quot;变量名&quot;) = 表达式 <br>
  &nbsp; Session(&quot;对象名称&quot;) = Server.CreateObject(ProgId) <br>
  例如Session(“name”)=“李明”。</p>
<p align="center"><img src="image/slide5image017.PNG" width="611" height="239"><br>
  <img src="image/slide5image018.PNG" width="614" height="272"></p>
<p>5.7.1设定Session对象变量的有效期限<br>
  &nbsp;&nbsp;&nbsp; 要更改Session对象的有效期限,只要设定TimeOut属性即可;TimeOut属性的默认值是20分钟。<br>
  5.7.2 Session对象的事件与Application对象一样,Session对象也有OnStart和OnEnd事件。<br>
  &nbsp;&nbsp;&nbsp; OnStart事件在客户第一 次从应用程序中请求ASP.NET网页时由ASP.NET调用,OnEnd事件在会话关闭时调用。 
  <br>
  5.8 Cookies对象<br>
  &nbsp;&nbsp;&nbsp; Cookies、Session和Application对象很类似,也是一种集合对象,都是用来在保存数据 。表列出对Application、Section及Cookies对象的比较。 
</p>
<p align="center"><img src="image/slide5image019.PNG" width="601" height="172"></p>
<p>5.8.1 Cookies对象的基本使用<br>
  Cookies对象不属于Page对象,所以用法和Application及Session对象不同。 语法:<br>
  &nbsp; Response.Cookies(Name As String).Value = 表达式 '创建Cookie变量 <br>
  &nbsp; Response.Cookies.Add(Cookie对象名)'写入Cookie对象 <br>
  &nbsp; 变量 = Request.Cookies(Name As String).Value '读取Cookie变量 例如:<br>
  &nbsp; Response.Cookies(&quot;userName&quot;).Value = &quot;mike&quot; <br>
  &nbsp; Response.Cookies(&quot;userName&quot;).Expires = DateTime.Now.AddDays(1) 
  <br>
  &nbsp;&nbsp;&nbsp; 以上语句创建名为userName的Cookie变量,并将该变量的有效期设置为1天。<br>
  再如:'创建名为CookieExmple的Cookie对象<br>
  &nbsp; Dim newCookie As HttpCookie = New HttpCookie(&quot;CookieExmple&quot;) 
  <br>
  &nbsp; newCookie.Values(&quot;Name&quot;) = &quot;Zhang Hua&quot; '添加名为Name、值为Zhang 
  Hua的子键 <br>
  &nbsp; newCookie.Values(&quot;Age&quot;) = &quot;21&quot; '添加名为Age、值为21的子键 <br>
  &nbsp; newCookie.Values(&quot;Visited&quot;) = DateTime.Now.Date()'添加名为Visited、值为当前日期的子键 
  <br>
  &nbsp; newCookie.Expires = DateTime.Now.AddDays(10) '设置Cookie的有效期 <br>
  &nbsp; Response.Cookies.Add(newCookie)'写入Cookie对象<br>
  &nbsp;&nbsp;&nbsp; 以上语句首先定义一个HttpCookie类对象newCookie,然后设置其各子键的名称和值,最后将通过Response对象将数据写入客户端。 
  <br>
  表分别列出了Cookies对象的常用属性和方法。</p>
<p align="center"><img src="image/slide5image020.PNG" width="601" height="172"><br>
  <img src="image/slide5image021.PNG" width="601" height="327"> </p>
<p>表列出了Cookie变量的常用属性。</p>
<p align="center"><img src="image/slide5image022.PNG" width="617" height="87"><br>
  <img src="image/slide5image023.PNG" width="618" height="101"></p>
<p>【例】下列程序新增两个Cookie变量,利用Cookies集合的Item属性和Get方法传回Cookie变量。<br>
  &lt;SCRIPT Language=&quot;VB&quot; Runat=&quot;Server&quot;&gt; <br>
  Sub Page_Load(Sender As Object,e As Eventargs) <br>
  &nbsp; Dim i As Short <br>
  &nbsp; Response.Cookies(&quot;Cookie1&quot;).Value=&quot;Microsoft Visual Studio.Net&quot; 
  <br>
  &nbsp; Response.Cookies(&quot;Cookie2&quot;).Value=&quot;ASP.Net&quot; <br>
  &nbsp; For i=0 To Request.Cookies.Count-1 <br>
  &nbsp;&nbsp; Response.Write(&quot;变量名称:&quot; &amp; Request.Cookies.Item(i).Name 
  &amp; _ &quot;&lt;BR&gt;变量内容:&quot; &amp; Request.Cookies.Get(i).Value &amp; 
  &quot;&lt;BR&gt;&quot;) <br>
  &nbsp; Next<br>
  &nbsp; Response.Cookies.Clear() <br>
  End Sub<br>
  &lt;/SCRIPT&gt;<br>
  5.8.2 自定义Cookies对象<br>
  &nbsp;&nbsp;&nbsp; 除了使用系统预设的Cookies对象外,还可以自行定义属于自己的Cookies对象。<br>
  【例】将上例程序中的Cookies改为自定义的Cookies对象来操作Cookie变量。<br>
  &lt;SCRIPT Language=&quot;VB&quot; Runat=&quot;Server&quot;&gt; <br>
  Sub Page_Load(Sender As Object,e As Eventargs) <br>
  &nbsp; Dim CookieCollection As HttpCookieCollection=New HttpCookieCollection 
  <br>
  &nbsp; Dim Cookie1 As HttpCookie=New HttpCookie(&quot;Cookie1&quot;) <br>
  &nbsp; Dim Cookie2 As HttpCookie=New HttpCookie(&quot;Cookie2&quot;) <br>
  &nbsp; Cookie1.Value=&quot;Microsoft VisualStudio .NET&quot; <br>
  &nbsp; Cookie2.Value=&quot;ASP.NET&quot; <br>
  &nbsp; CookieCollection.Add(Cookie1) '将Cookie 对象加入Cookies 集合中 <br>
  &nbsp; CookieCollection.Add(Cookie2) <br>
  &nbsp; Dim i As Short <br>
  &nbsp; For i=0 To CookieCollection.Count-1 <br>
  &nbsp;&nbsp; Response.Write(&quot;变量名称=&quot; &amp; CookieCollection.Item(I).Name 
  &amp; &quot;&lt;BR&gt;&quot;) <br>
  &nbsp;&nbsp; Response.Write(&quot;变量内容=&quot; &amp; CookieCollection.Get(I).Value 
  &amp; &quot;&lt;P&gt;&quot;) <br>
  &nbsp; Next <br>
  &nbsp; CookieCollection.Clear() <br>
  End Sub <br>
  &lt;/SCRIPT&gt;<br>
  5.8.3 设定Cookie变量的生命周期Cookie变量的生命周期起始于浏览器被执行时,终止于浏览器结束执行时。 语法: <br>
  &nbsp; Response.Cookies(CookieName).Expires=#日期# 例如:<br>
  &nbsp; Response.Cookies(myCookie).Expires=“#2004/12/31#”,设置Cookie变量在2004年12月31日失效。<br>
  若没有指定Expires属性,则Cookie变量将不会被存储。</p>
<p align="center"><a href="index.htm">回首页</a></p>
</body>
</html>

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -