ASP常用的代码(不断更新)

1.获得系统时间:

1=now()

2.取得来访用的IP:

1=request.serverVariables("remote_host")

3.获得系统,浏览器版本:

1<script>   
2window.document.write("版本:"+navigator.appName+navigator.appVersion+" browser.")   
3</script>

4.去除IE混动条:

 1<body scroll="no">
 2<body style="overflow-y:hidden">   
 35.进入网站,跳出广告:   
 4<script language="javascript">   
 5<!--   
 6<!-- 注意更改文件所在路径-->   
 7window.open(''http://www.XXXXXX.com'','''',''height=200,width=300,top=0,left=30'');   
 8// -->   
 9</script>   
106.随机数:   

randomize

=(int(rnd()*n)+1)

 1N为可改变数   
 27.向上混动代码:   
 3<marquee bgcolor="#FFFF00" direction="up" height="207" scrollamount="2" scrolldelay="200" style="font-size: 9pt; color: #FF0000; line-height: 150%; font-style:italic; font-weight:bold" width="206">hhhhhhhhhhhhhhhhhhh</marquee>   
 48.自动关闭网页:   
 5<script language="javascript">   
 6<!--   
 7setTimeout(''window.close();'', 10000); //60秒后关闭   
 8// -->   
 9</script>
10<p align="center">本页10秒后自动关闭,请注意刷新页面</p>   
119.随机背景音乐:   

randomize

 1<bgsound loop="-1" src="mids/```
 2=(int(rnd()*60)+1)
 3```.mid"/>   
 4  
 5可以修改数字,限制调用个数,我这里是60个.   
 610.自动刷新本页面:   
 7<script>   
 8<!--   
 9  
10var limit="0:10"   
11  
12if (document.images){   
13var parselimit=limit.split(":")   
14parselimit=parselimit[0]*60+parselimit[1]*1   
15}   
16function beginrefresh(){   
17if (!document.images)   
18return   
19if (parselimit==1)   
20window.location.reload()   
21else{   
22parselimit-=1   
23curmin=Math.floor(parselimit/60)   
24cursec=parselimit%60   
25if (curmin!=0)   
26curtime=curmin+"分"+cursec+"秒后重刷本页!"   
27else   
28curtime=cursec+"秒后重刷本页!"   
29window.status=curtime   
30setTimeout("beginrefresh()",1000)   
31}   
32}   
33  
34window.onload=beginrefresh   
35file://-->   
36</script>   
3711.ACCESS数据库连接:   

option explicit
dim startime,endtime,conn,connstr,db
startime=timer()
'更改数据库名字
db="data/dvBBS5.mdb"
Set conn = Server.createObject("ADODB.Connection")
connstr="Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" &amp; Server.MapPath(db)
'如果你的服务器采用较老版本Access驱动,请用下面连接方法
'connstr="driver={Microsoft Access Driver (*.mdb)};dbq=" &amp; Server.MapPath(db)
conn.Open connstr
function CloseDatabase
Conn.close
Set conn = Nothing
End Function

112.SQL数据库连接:   

option explicit
dim startime,endtime,conn,connstr,db
startime=timer()
connstr="driver={SQL Server};server=HUDENQ-N11T33NB;uid=sa;pwd=xsfeihu;database=dvbbs"
Set conn = Server.createObject("ADODB.Connection")
conn.Open connstr
function CloseDatabase
Conn.close
Set conn = Nothing
End Function

  113.用键盘打开网页代码:   
  2<script language="javascript">   
  3function ctlent(eventobject)   
  4{   
  5if((event.ctrlKey && window.event.keyCode==13)||(event.altKey && window.event.keyCode==83))   
  6{   
  7window.open('网址','','')   
  8}   
  9}   
 10</script>   
 11  
 12这里是Ctrl+Enter和Alt+S的代码 自己查下键盘的ASCII码再换就行   
 1314.让层不被控件复盖代码:   
 14<div z-index:2=""><object ***=""></object></div> # 前面   
 15<div z-index:1=""><object ***=""></object></div> # 后面   
 16<div id="Layer2" style="position:absolute; top:40;width:400px; height:95px;z-index:2"><table bgcolor="#ff0000" height="100%" width="100%"><tr><td height="100%" width="100%"></td></tr></table><iframe height="0" width="0"></iframe></div>
 17<div id="Layer1" style="position:absolute; top:50;width:200px; height:115px;z-index:1"><iframe height="100%" width="100%"></iframe></div>   
 18  
 1915.动网FLASH广告代码:   
 20<object classid="clsid27CDB6E-AE6D-11cf-96B8-444553540000" codebase="http://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=5,0,0,0" height="60" width="468"><param name="movie" value="images/yj16d.swf"/><param name="quality" value="high"/><embed ;;;;="" height="60" pluginspage="http://www.macromedia.com/shockwave/download/index.cgi?P1_Prod_Version=ShockwaveFlash" quality="high" src="images/dvbanner.swf" type="application/x-shockwave-flash" width="468"/></object>   
 2116.VBS弹出窗口小代码:   
 22<script language="vbscript">   
 23msgbox"你还没有注册或登陆论坛","0","精品论坛"   
 24location.href = "login.asp"   
 25</script>   
 2616.使用FSO修改文件特定内容的函数   
 27function FSOchange(filename,Target,String)   
 28Dim objFSO,objCountFile,FiletempData   
 29Set objFSO = Server.createObject("Scripting.FileSystemObject")   
 30Set objCountFile = objFSO.OpenTextFile(Server.MapPath(filename),1,True)   
 31FiletempData = objCountFile.ReadAll   
 32objCountFile.Close   
 33FiletempData=Replace(FiletempData,Target,String)   
 34Set objCountFile=objFSO.createTextFile(Server.MapPath(filename),True)   
 35objCountFile.Write FiletempData   
 36objCountFile.Close   
 37Set objCountFile=Nothing   
 38Set objFSO = Nothing   
 39End Function   
 4017.使用FSO读取文件内容的函数   
 41function FSOFileRead(filename)   
 42Dim objFSO,objCountFile,FiletempData   
 43Set objFSO = Server.createObject("Scripting.FileSystemObject")   
 44Set objCountFile = objFSO.OpenTextFile(Server.MapPath(filename),1,True)   
 45FSOFileRead = objCountFile.ReadAll   
 46objCountFile.Close   
 47Set objCountFile=Nothing   
 48Set objFSO = Nothing   
 49End Function   
 5018.使用FSO读取文件某一行的函数   
 51function FSOlinedit(filename,lineNum)   
 52if linenum &lt; 1 then exit function   
 53dim fso,f,temparray,tempcnt   
 54set fso = server.createObject("scripting.filesystemobject")   
 55if not fso.fileExists(server.mappath(filename)) then exit function   
 56set f = fso.opentextfile(server.mappath(filename),1)   
 57if not f.AtEndofStream then   
 58tempcnt = f.readall   
 59f.close   
 60set f = nothing   
 61temparray = split(tempcnt,chr(13)&amp;chr(10))   
 62if lineNum&gt;ubound(temparray)+1 then   
 63exit function   
 64else   
 65FSOlinedit = temparray(lineNum-1)   
 66end if   
 67end if   
 68end function   
 6919.使用FSO写文件某一行的函数   
 70function FSOlinewrite(filename,lineNum,Linecontent)   
 71if linenum &lt; 1 then exit function   
 72dim fso,f,temparray,tempCnt   
 73set fso = server.createObject("scripting.filesystemobject")   
 74if not fso.fileExists(server.mappath(filename)) then exit function   
 75set f = fso.opentextfile(server.mappath(filename),1)   
 76if not f.AtEndofStream then   
 77tempcnt = f.readall   
 78f.close   
 79temparray = split(tempcnt,chr(13)&amp;chr(10))   
 80if lineNum&gt;ubound(temparray)+1 then   
 81exit function   
 82else   
 83temparray(lineNum-1) = lineContent   
 84end if   
 85tempcnt = join(temparray,chr(13)&amp;chr(10))   
 86set f = fso.createtextfile(server.mappath(filename),true)   
 87f.write tempcnt   
 88end if   
 89f.close   
 90set f = nothing   
 91end function   
 9220.使用FSO添加文件新行的函数   
 93function FSOappline(filename,Linecontent)   
 94dim fso,f   
 95set fso = server.createObject("scripting.filesystemobject")   
 96if not fso.fileExists(server.mappath(filename)) then exit function   
 97set f = fso.opentextfile(server.mappath(filename),8,1)   
 98f.write chr(13)&amp;chr(10)&amp;Linecontent   
 99f.close   
100set f = nothing   
101end function   
10221.读文件最后一行的函数   
103function FSOlastline(filename)   
104dim fso,f,temparray,tempcnt   
105set fso = server.createObject("scripting.filesystemobject")   
106if not fso.fileExists(server.mappath(filename)) then exit function   
107set f = fso.opentextfile(server.mappath(filename),1)   
108if not f.AtEndofStream then   
109tempcnt = f.readall   
110f.close   
111set f = nothing   
112temparray = split(tempcnt,chr(13)&amp;chr(10))   
113FSOlastline = temparray(ubound(temparray))   
114end if   
115end function   
11622.利用FSO取得BMP,JPG,PNG,GIF文件信息(大小,宽、高等)   

'::: BMP, GIF, JPG and PNG :::
'::: This function gets a specified number of bytes from any :::
'::: file, starting at the offset (base 1) :::
'::: :::
'::: Passed: :::
'::: flnm =&gt; Filespec of file to read :::
'::: offset =&gt; Offset at which to start reading :::
'::: bytes =&gt; How many bytes to read :::
'::: :::
':::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
function GetBytes(flnm, offset, bytes)
Dim objFSO
Dim objFTemp
Dim objTextStream
Dim lngSize
on error resume next
Set objFSO = createObject("Scripting.FileSystemObject")

' First, we get the filesize
Set objFTemp = objFSO.GetFile(flnm)
lngSize = objFTemp.Size
set objFTemp = nothing
fsoForReading = 1
Set objTextStream = objFSO.OpenTextFile(flnm, fsoForReading)
if offset &gt; 0 then
strBuff = objTextStream.Read(offset - 1)
end if
if bytes = -1 then ' Get All!
GetBytes = objTextStream.Read(lngSize) 'ReadAll
else
GetBytes = objTextStream.Read(bytes)
end if
objTextStream.Close
set objTextStream = nothing
set objFSO = nothing
end function

':::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
'::: :::
'::: Functions to convert two bytes to a numeric value (long) :::
'::: (both little-endian and big-endian) :::
'::: :::
':::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
function lngConvert(strTemp)
lngConvert = clng(asc(left(strTemp, 1)) + ((asc(right(strTemp, 1)) * 256)))
end function
function lngConvert2(strTemp)
lngConvert2 = clng(asc(right(strTemp, 1)) + ((asc(left(strTemp, 1)) * 256)))
end function

':::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
'::: :::
'::: This function does most of the real work. It will attempt :::
'::: to read any file, regardless of the extension, and will :::
'::: identify if it is a graphical image. :::
'::: :::
'::: Passed: :::
'::: flnm =&gt; Filespec of file to read :::
'::: width =&gt; width of image :::
'::: height =&gt; height of image :::
'::: depth =&gt; color depth (in number of colors) :::
'::: strImageType=&gt; type of image (e.g. GIF, BMP, etc.) :::
'::: :::
':::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
function gfxSpex(flnm, width, height, depth, strImageType)
dim strPNG
dim strGIF
dim strBMP
dim strType
strType = ""
strImageType = "(unknown)"
gfxSpex = False
strPNG = chr(137) &amp; chr(80) &amp; chr(78)
strGIF = "GIF"
strBMP = chr(66) &amp; chr(77)
strType = GetBytes(flnm, 0, 3)
if strType = strGIF then ' is GIF
strImageType = "GIF"
Width = lngConvert(GetBytes(flnm, 7, 2))
Height = lngConvert(GetBytes(flnm, 9, 2))
Depth = 2 ^ ((asc(GetBytes(flnm, 11, 1)) and 7) + 1)
gfxSpex = True
elseif left(strType, 2) = strBMP then ' is BMP
strImageType = "BMP"
Width = lngConvert(GetBytes(flnm, 19, 2))
Height = lngConvert(GetBytes(flnm, 23, 2))
Depth = 2 ^ (asc(GetBytes(flnm, 29, 1)))
gfxSpex = True
elseif strType = strPNG then ' Is PNG
strImageType = "PNG"
Width = lngConvert2(GetBytes(flnm, 19, 2))
Height = lngConvert2(GetBytes(flnm, 23, 2))
Depth = getBytes(flnm, 25, 2)
select case asc(right(Depth,1))
case 0
Depth = 2 ^ (asc(left(Depth, 1)))
gfxSpex = True
case 2
Depth = 2 ^ (asc(left(Depth, 1)) * 3)
gfxSpex = True
case 3
Depth = 2 ^ (asc(left(Depth, 1))) '8
gfxSpex = True
case 4
Depth = 2 ^ (asc(left(Depth, 1)) * 2)
gfxSpex = True
case 6
Depth = 2 ^ (asc(left(Depth, 1)) * 4)
gfxSpex = True
case else
Depth = -1
end select

else
strBuff = GetBytes(flnm, 0, -1) ' Get all bytes from file
lngSize = len(strBuff)
flgFound = 0
strTarget = chr(255) &amp; chr(216) &amp; chr(255)
flgFound = instr(strBuff, strTarget)
if flgFound = 0 then
exit function
end if
strImageType = "JPG"
lngPos = flgFound + 2
ExitLoop = false
do while ExitLoop = False and lngPos &lt; lngSize

do while asc(mid(strBuff, lngPos, 1)) = 255 and lngPos &lt; lngSize
lngPos = lngPos + 1
loop
if asc(mid(strBuff, lngPos, 1)) &lt; 192 or asc(mid(strBuff, lngPos, 1)) &gt; 195 then
lngMarkerSize = lngConvert2(mid(strBuff, lngPos + 1, 2))
lngPos = lngPos + lngMarkerSize + 1
else
ExitLoop = True
end if
loop
'
if ExitLoop = False then
Width = -1
Height = -1
Depth = -1
else
Height = lngConvert2(mid(strBuff, lngPos + 4, 2))
Width = lngConvert2(mid(strBuff, lngPos + 6, 2))
Depth = 2 ^ (asc(mid(strBuff, lngPos + 8, 1)) * 8)
gfxSpex = True
end if

end if
end function

':::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
'::: Test Harness :::
':::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::

' To test, we'll just try to show all files with a .GIF extension in the root of C:
Set objFSO = createObject("Scripting.FileSystemObject")
Set objF = objFSO.GetFolder("c:")
Set objFC = objF.Files
response.write "<table 0""="" 5""="" border="" cellpadding="">"
For Each f1 in objFC
if instr(ucase(f1.Name), ".GIF") then
response.write "<tr><td>" &amp; f1.name &amp; "</td><td>" &amp; f1.Datecreated &amp; "</td><td>" &amp; f1.Size &amp; "</td><td>"
if gfxSpex(f1.Path, w, h, c, strType) = true then
response.write w &amp; " x " &amp; h &amp; " " &amp; c &amp; " colors"
else
response.write " "
end if
response.write "</td></tr>"
end if
Next
response.write "</table>"
set objFC = nothing
set objF = nothing
set objFSO = nothing

 123.点击返回上页代码:   
 2<form>
 3<p><input onclick="history.back(-1)" type="button" value="返回上一步"/></p>
 4</form>   
 524.点击刷新代码㈠:   
 6<form>
 7<p><input onclick="ReloadButton()" type="button" value="刷新按钮一"/></p>
 8</form>
 9<script language="javascript"><!--   
10function ReloadButton(){location.href="allbutton.htm";}   
11  
1224.点击刷新代码㈡:   
13<form>   
14<p><input TYPE="button" value="刷新按钮二" onClick="history.go(0)"> </p>   
15</form>   
16  
17<form>   
18<p><input TYPE="button" value="打开一个网站" onCLICK="HomeButton()"></p>   
19</form>   
20<script language="javascript"><!--   
21function HomeButton(){location.href="http://nettrain.126.com";;;;}   
22// --></script>   
23  
2425.弹出警告框代码:   
25<form>
26<p><input onclick="AlertButton()" type="button" value="弹出警告框"/></p>
27</form>
28<script language="javascript"><!--   
29function AlertButton(){window.alert("要多多光临呀!");}   
30// --></script>   
31  
3226.状态栏信息   
33<form>
34<p><input onclick="StatusButton()" type="button" value="状态栏信息"/></p>
35</form>
36<script language="javascript"><!--   
37function StatusButton(){window.status="要多多光临呀!";}   
38// --></script>   
39  
4027.背景色变换   
41<form>
42<p><input onclick="BgButton()" type="button" value="背景色变换"/></p>
43</form>
44<script>function BgButton(){   
45if (document.bgColor=='#00ffff')   
46{document.bgColor='#ffffff';}   
47else{document.bgColor='#00ffff';}   
48}   
49</script>   
50  
5128.点击打开新窗口   
52<form>
53<p><input onclick="NewWindow()" type="button" value="打开新窗口"/></p>
54</form>
55<script language="javascript"><!--   
56function NewWindow(){window.open("http://www.mcmx.com";;;,"","height=240,width=340,status=no,location=no,toolbar=no,directories=no,menubar=no");}   
57// --></script></body>   
5829.分页代码:   

''本程序文件名为:Pages.asp

''包含ADO常量表文件adovbs.inc,可从"\Program Files\Common Files\System\ADO"目录下拷贝

1<!--#Include File="adovbs.inc"-->   

''*建立数据库连接,这里是Oracle8.05数据库
Set conn=Server.createObject("ADODB.Connection")
conn.Open "Provider=msdaora.1;Data Source=YourSrcName;User ID=YourUserID;Password=YourPassword;"

Set rs=Server.createObject("ADODB.Recordset") ''创建Recordset对象
rs.CursorLocation=adUseClient ''设定记录集指针属性
''*设定一页内的记录总数,可根据需要进行调整
rs.PageSize=10

''*设置查询语句
StrSQL="select ID,姓名,住址,电话 from 通讯录 Order By ID"
rs.Open StrSQL,conn,adOpenStatic,adLockReadOnly,adCmdText

 1<html>
 2<head>
 3<title>分页示例</title>
 4<script language="javascript">   
 5//点击"[第一页]"时响应:   
 6function PageFirst()   
 7{   
 8document.MyForm.CurrentPage.selectedIndex=0;   
 9document.MyForm.CurrentPage.onchange();   
10}   
11//点击"[上一页]"时响应:   
12function PagePrior()   
13{   
14document.MyForm.CurrentPage.selectedIndex--;   
15document.MyForm.CurrentPage.onchange();   
16}   
17//点击"[下一页]"时响应:   
18function PageNext()   
19{   
20document.MyForm.CurrentPage.selectedIndex++;   
21document.MyForm.CurrentPage.onchange();   
22}   
23//点击"[最后一页]"时响应:   
24function PageLast()   
25{   
26document.MyForm.CurrentPage.selectedIndex=document.MyForm.CurrentPage.length-1;   
27document.MyForm.CurrentPage.onchange();   
28}   
29//选择"第?页"时响应:   
30function PageCurrent()   
31{ //Pages.asp是本程序的文件名   
32document.MyForm.action='Pages.asp?Page='+(document.MyForm.CurrentPage.selectedIndex+1)   
33document.MyForm.submit();   
34}   
35</script>
36</head>
37<body "="" alink="#FF0000" bgcolor="#ffffcc" link="#008000" vlink="#008000">   
38  

IF rs.Eof THEN
Response.Write("<font color="#000080" size="2">[数据库中没有记录!]</font>")
ELSE
''指定当前页码
If Request("CurrentPage")="" Then
rs.AbsolutePage=1
Else
rs.AbsolutePage=CLng(Request("CurrentPage"))
End If

''创建表单MyForm,方法为Get
Response.Write("<form method="Get" name="MyForm">")
Response.Write("<p align="center"><font color="#008000" size="2">")
''设置翻页超链接
if rs.PageCount=1 then
Response.Write("[第一页] [上一页] [下一页] [最后一页] ")
else
if rs.AbsolutePage=1 then
Response.Write("[第一页] [上一页] ")
Response.Write("[<a href="java" script:pagenext()="">下一页</a>] ")
Response.Write("[<a href="java" script:pagelast()="">最后一页</a>] ")
else
if rs.AbsolutePage=rs.PageCount then
Response.Write("[<a href="java" script:pagefirst()="">第一页</a>] ")
Response.Write("[<a href="java" script:pageprior()="">上一页</a>] ")
Response.Write("[下一页] [最后一页] ")
else
Response.Write("[<a href="java" script:pagefirst()="">第一页</a>] ")
Response.Write("[<a href="java" script:pageprior()="">上一页</a>] ")
Response.Write("[<a href="java" script:pagenext()="">下一页</a>] ")
Response.Write("[<a href="java" script:pagelast()="">最后一页</a>] ")
end if
end if
end if

''创建下拉列表框,用于选择浏览页码
Response.Write("第<select name="CurrentPage" onchange="PageCurrent()" size="1">")
For i=1 to rs.PageCount
if rs.AbsolutePage=i then
Response.Write("<option selected="">"&amp;i&amp;"</option>") ''当前页码
else
Response.Write("<option>"&amp;i&amp;"</option>")
end if
Next
Response.Write("</select>页/共"&amp;rs.PageCount&amp;"页 共"&amp;rs.RecordCount&amp;"条记录</font><p>")
Response.Write("</p></p></form>")

''创建表格,用于显示
Response.Write("<table align="center" border='1")' bordercolor="#99CCFF" bordercolordark="#b0e0e6" bordercolorlight="#000066" cellpadding="1" cellspacing="1" response.write("="">")

Response.Write("<tr bgcolor="#ccccff" bordercolor="#000066">")

Set Columns=rs.Fields

''显示表头
For i=0 to Columns.Count-1
Response.Write("<td align="center" height="13" width="200">")
Response.Write("<font size="2"><b>"&amp;Columns(i).name&amp;"</b></font></td>")
Next
Response.Write("</tr>")
''显示内容
For i=1 to rs.PageSize
Response.Write("<tr bgcolor="#99ccff" bordercolor="#000066">")
For j=0 to Columns.Count-1
Response.Write("<td><font size="2">"&amp;Columns(j)&amp;"</font></td>")
Next
Response.Write("</tr>")

rs.movenext
if rs.EOF then exit for
Next

Response.Write("</table>")

END IF

1</body>
2</html></body>
Published At
Categories with Web编程
Tagged with
comments powered by Disqus