ASP常见问题及解答(7)

80酷酷网    80kuku.com

  解答|问题1.分 类:语法应用
问题描述:查找两个字符串间的字符出现次数
解决办法:

function FindStringTimes(strContent,start_string,end_string) 'start_string 与 end_string 之间字符出次的数
'written by Jaron
FindStringTimes = ubound(split(strContent,start_string))
end function

示例: 查找以下字符串有多少张图片
response.write FindStringTimes(HTML,"<img",">")
<img src=http://www.163design.net/a/q/"a.jpg"><img src=http://www.163design.net/a/q/"b.jpg"><img src=http://www.163design.net/a/q/"d.jpg"><img src=http://www.163design.net/a/q/"c.jpg">



2.遍历目录以及目录下文件的函数

<%
function bianli(path)
set fso=server.CreateObject("scripting.filesystemobject")

on error resume next
set objFolder=fso.GetFolder(path)

set objSubFolders=objFolder.Subfolders

for each objSubFolder in objSubFolders

nowpath=path + "\" + objSubFolder.name

Response.Write nowpath

set objFiles=objSubFolder.Files

for each objFile in objFiles
Response.Write "<br/>---"
Response.Write objFile.name
next
Response.Write "<p>"
bianli(nowpath)'递归

next
set objFolder=nothing
set objSubFolders=nothing
set fso=nothing
end function
%>
<%
bianli("d:") '遍历d:盘
%>




3.生成一个不重复的随即数字

Sub CalCaPiao()
Dim strCaiPiaoNoArr() As String
Dim strSQL As String
Dim strCaiPiaoNo As String
strCaiPiaoNo = "01,02,03,04,05,06,07,08,09,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24,25,26,27,28,29,30,31,32,33"
Dim StrTempArr(7) As String
Dim strZhongJiangArr(7) As String
strCaiPiaoNoArr = Split(strCaiPiaoNo, ",")
Dim intRand As Integer
Dim i As Integer
Dim j As Integer
i = 0
Dim find As Boolean
Do While True
find = False
Randomize
intRand = Int((33 * Rnd) + 1)
For j = 0 To i - 1
If StrTempArr(j) = CStr(intRand) Then
find = True
End If
Next
If Not find Then
StrTempArr(j) = CStr(intRand)
strZhongJiangArr(i) = CStr(intRand)
'Text1(i) = strZhongJiangArr(i)
i = i + 1
If i = 7 Then
Exit Do
End If
End If
Loop
End Sub



4.下载任何文件(尤其是IE关联打开的)
<%
Dim Stream
Dim Contents
Dim FileName
Dim FileExt
Const adTypeBinary = 1
FileName = Request.QueryString("FileName")
if FileName = "" Then
Response.Write "无效文件名."
Response.End
End if
' 下面是不希望下载的文件
FileExt = Mid(FileName, InStrRev(FileName, ".") + 1)
Select Case UCase(FileExt)
Case "ASP", "ASA", "ASPX", "ASAX", "MDB"
Response.Write "受保护文件,不能下载."
Response.End
End Select
' 下载这个文件
Response.Clear
Response.ContentType = "application/octet-stream"
Response.AddHeader "content-disposition", "attachment; filename=" & FileName
Set Stream = server.CreateObject("ADODB.Stream")
Stream.Type = adTypeBinary
Stream.Open
Stream.LoadFromFile Server.MapPath(FileName)
While Not Stream.EOS
Response.BinaryWrite Stream.Read(1024 * 64)
Wend
Stream.Close
Set Stream = Nothing
Response.Flush
Response.End
%>


5.背景色变换
<form>
<p><input TYPE="button" value="背景色变换" onClick="BgButton()"></p>
</form>
<script>function BgButton(){
if (document.bgColor==#00ffff)
{document.bgColor=#ffffff;}
else{document.bgColor=#00ffff;}
}
</script> 



6.点击打开新窗口
<form>
<p><input TYPE="button" value="打开新窗口" onCLICK="NewWindow()"></p>
</form>
<script language="javascript"><!--
function NewWindow(){window.open("http://www.mcmx.com";;;,"","height=240,width=340,status=no,location=no,toolbar=no,directories=no,menubar=no");}
// --></script></body>



7.分页代码:
<%本程序文件名为:Pages.asp%>
<%包含ADO常量表文件adovbs.inc,可从"\Program Files\Common Files\System\ADO"目录下拷贝%>
<!--#Include File="adovbs.inc"-->
<%*建立数据库连接,这里是Oracle8.05数据库
Set conn=Server.CreateObject("ADODB.Connection")
conn.Open "Prov

分享到
  • 微信分享
  • 新浪微博
  • QQ好友
  • QQ空间
点击: