推薦:一些值得收藏的ASP代碼值得收藏的一些ASP代碼值得收藏的一些ASP代碼 1. oncontextmenu=window.event.returnvalue=false 將徹底屏蔽鼠標右鍵 table border oncontextmenu=return(false)tdno/table 可用于Table 2. body onselectstart=return false 取消選
為了能夠正常使用,必須把它們放到服務器上的一個虛擬應用程序內,并且把所提供的global.asa文件放到該應用程序的根目錄中。最簡單的辦法是把global.asa文件放到缺省Web網站的根目錄(缺省情況下是C:/InetPub/WWWRoot)中。
對任何已有的global.asa文件重命名是一個好辦法,可以在以后對該文件進行恢復。
1. 顯示Application集合的內容
ASPCounter對象是StaticObjects集合的一個成員(通過<OBJECT>元素進行定義),但是其余部份(由Server.CreateObject實例化)是Contents集合的成員。
可以看到使用global.asa例子網頁放到這些集合中的值,這在前面已經看到:
以下為引用的內容: <!-- Declare instance of the ASPCounter component with application-level scope //--> <OBJECT ID=”ASPCounter” RUNAT=”Server” SCOPE=”Applicatoin” PROGID=”MSWC.Counters”> </OBJECT> ... ... <SCRIPT LANGUAGE=”VBScript” RUNAT=”Server”> Sub Application_onStart() ‘Create an instance of an ADO Connection with application-level scope Set Application(“ADOConnection”) = Server.CreateObject(“ADODB.Connection”) 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) = “Application object” Application(“Variant_Array”) = varArray ‘Store it in thd Application Application(“Start_Time”) = CStr(Now) ‘Store the date/time as a string Application(“Visit_Count”) = 0 ‘Set counter variable to zero End Sub ... ... </SCRIPT> |
(1) 遍歷Contents集合的代碼
為了遍歷Contents集合,可使用一個For Each ... Next結構。集合中的每一項可以是一個簡單的Variant類型變量、一個Variant數組或者一個對象的引用。因為需要對每種類型的值進行不同的處理,所以就不得不對每一個進行檢查來判別其類型。
在VBScript中可使用VarType函數完成這個工作。這里使用IsObject和IsArray函數代替:
以下為引用的內容: For Each objItem in Application.Contents If IsObject(Application.Contents(objItem)) Then Response.Write “Object reference: ‘” & objItem & “’ Response.Write “Array: ‘” & objItem & “’ contents are: ‘Note: the following only works with a one-dimensional array For intLoop = 0 To UBound(varArray) Response.Write “ Index(“ & intLoop & “) = “ & _ VarArray(intLoop) & “ ” Else Response.Write “Variable: ‘” & objItem & “’ = “ _ & Application.Contents(objItem) & “ ” Next |
注意程序如何從Application對象檢索該數組。將其分配給一個局部(Variant)變量,使用下面的語句:
varArray = Application.Contents(objItem)
使用UBound函數可以查找出數組的大小(元素的數量),這個值可以作為遍歷的終止條件:
For intLoop = 0 UBound(varArray)
這個例子是一維數組,并將只顯示這樣的一個數組的內容。可根據需要編輯代碼以處理多維數組,例如:
以下為引用的內容: For intLoop = 0 To UBound(varArray) IntNumberOfDimensions = UBound(varArray, 1) For intDimension = 0 To intNumberOfDimensions Response.Write “ Index(“ & intLoop & “) = “ _ & varArray(intLoop, intDimension) Next Response.Write “” Next |
(2) 遍歷StaticObjects集合的代碼
StaticObjects集合包含了所有在global.asa中使用<OBJECT>元素聲明的對象引用。因為每個條目都是一個對象變量,可用簡單些的代碼對這個數組進行遍歷。我們將輸出對象的名字(在ID屬性中原有的定義):
以下為引用的內容: For Each objItem in Application.StaticObjects If IsObject(Application.StaticObjects(objItem)) Then Response.Write “<OBJECT> element: ID=’” & objItem & “’ Next |
分享:ASP編程中11種數據庫的常用語法本文主要介紹了ASP連接11種數據庫的常用語法,詳細內容請參考下文: 1.Access數據庫的DSN-less連接方法: 以下為引用的內容: set adocon=Server.Createobject(adodb.connection) adoconn.OpenDriver={Microsoft Access Driver(*.mdb)};DBQ= _ Server.MapPath
新聞熱點
疑難解答