大纲: 简介面向对象程序 Response类对象及对象的搜寻 Request类对象 Page类对象 Application类对象 Session类对象及Cookie类对象 StateBag类对象 Server类对象简介面向对象程序:Class(对象类): 构造对象的模型Class中有4种member(成员): Constructor(类对象起始程序)、Property(对象属性)、Method(对象方法)、Event(对象事件)Constructor的语法: sub new() 设定各property的初始值 end subClass的程序举例:
<%@ Page Language="VB"%> <script runat="server"> class Clock public Second as integer public Minute as integer public Hour as integer public sub SetTime(intSec as integer,intMin as integer,intHour as integer) Second = intSec Minute = intMin Hour = intHour end sub end class sub Page_Load(obj as Object,e as EventArgs) dim objStartClock as new Clock dim objEndClock as new Clock objStartClock.SetTime(4,6,7) objEndClock.SetTime(3,20,7) Response.write("The starting time is:") Response.write(objStartClock.Hour & ":" & _ objStartClock.Minute & ":" & _ objStartClock.Second & "<br>") Response.write("The ending time is:") Response.write(objEndClock.Hour & ":" & _ objEndClock.Minute & ":" & _ objEndClock.Second) end sub </script>Response类对象及对象的搜寻Request类对象常用的MethodsBinaryRead 传回一个阵列,此阵列为传给server的资料,其数据类型为ByteMapImageCoordinates 取得Image的坐标MapPath 将虚拟路径转换成实际路径SaveAs 将HTTP request存至硬盘中Request物件常用的propertiesPage物件常用的propertiesPage物件常用的MethodsDataBind 做此网页所有controls的data binding(资料结合)FindControl 找出某种ControlLoadControl 从.aspx文件中载入User ControlLoadTemplate 载入一个TemplateMapPath 取得虚拟路径的实际路径ResolveURL 将虚拟的URL转换成实际的URLValidate 指示某个validation control去验证其负责的controlPage物件常用的事件AbortTransaction 当该page中的Transaction被Abort时CommitTransaction 当该page中的Transaction被Commit时Error 当处理page时发生错误时Init 当page页面第一次被执行时Load 每当page被载入而且所有的控制标签都被载入之后PreRender 每当信息写入用户端之前Unload 当网页传送给client端之后事件执行顺序:Init -> Load -> 控制标签发出的事件 -> PreRender -> Unload
Application物件:
<%@ Page Language="VB"%> <script runat="server"> sub Page_Load(obj as object,e as eventargs) Application.Lock Application("Counter")=Application("Counter") + 1 Application.Unlock lblMessage.Text = "你是第" & Application("Counter") & "位访客" end sub </script> <html> <body> <asp:Label ID="lblMessage" runat="server" /> </body> </html>Session物件及Cookie物件:Session物件:指使用者浏览网页的那段时间,即从使用者浏览第一个网页开始,到离开那个网页止,这段时间就是SessionCookie物件:提供在用户(client)端建立一个小档案,用来储存使用者浏览此网页时的一些基本资料,这个小档案就是Cookie
Application、Session、Cookie物件之间的区别比较:Application定义的变量不管使用者有多少,都使用共同的变量,有点类似全局变量,并且这些变量是一直储存在系统中的Session定义的变量,是每个使用者各有一份,自己使用自己的,生命周期随Session的结束而消失Cookie的资料信息是存放在使用者的客户端机器中,而Application和Session的则是存放在服务器中,这些信息不会因为浏览的结束而消失,除非特别设定,通常会保留1000分钟
例:Session("UserName")="Jordan" Name = Session("UserName") Session.Add("UserName","Jordan") Response.Cookies("UserName").Value = "Jordan" Response.Cookies("Age").Value=38 Name = Request.Cookies("UserName") Response.Cookies("UserRecord")("UserName") = "Jordan" Response.Cookies("UserRecord")("Age") = 38
StateBag物件: StateBag物件也能储存变量,其生命周期随Page的结束而消失
<%@ Page Language="VB" %> <script runat="server"> sub Page_Load(obj as Object,e as EventArgs) if not Page.IsPostBack then ViewState("ArrivalTime") = DateTime.Now end if end sub sub Submit(obj as Object,e as EventArgs) Response.write("你到达的时间是" & ViewState("ArrivalTime") ) Response.write("<br>目前的时间是:" & DateTime.Now) end sub </script> <html> <body> <form runat="server"> <asp:Button Text="显示我进入此网页的时间" Onclick="Submit" Runat="server" /> </form> </body> </html>Server物件中常用的properties及methods例:
<script language="vb" runat="server"> sub Page_Load(obj as object,e as EventArgs) Response.write("HtmlDecode:" & Server.HtmlDecode("<b>Hi</b>") & "<br>" ) Response.write("HtmlEncode:" & Server.HtmlEncode("<b>Hi</b>") ) end sub </script>网页显示: HtmlDecode:Hi HtmlEncode:<b>Hi</b>