iLeichun

当前位置:首页ASP

ASP实现分页代码

分类:ASP  来源:网络  时间:2011-3-12 12:34:55

此效果最后的显示是:第N页[共*页] <<1 2 3 4 5 6 7 8 9 10 >>。

用DW+ASP做网页时,在绑定记录集后,代码页里马上出现以下代码:

以下是引用片段:
<% 
Dim Recordset1 
Dim Recordset1_numRows 
Set Recordset1 = Server.CreateObject("ADODB.Recordset") 
Recordset1.ActiveConnection = MM_数据库名_STRING 
Recordset1.Source = "SELECT * FROM 表名" 
Recordset1.CursorType = 0 
Recordset1.CursorLocation = 2 
Recordset1.LockType = 1 
Recordset1.Open() 
Recordset1_numRows = 0 
%> 


  现在我们要来对代码做点修改,请在上面代码中修改为如下的代码:

以下是引用片段:
<% 
Dim I 
Dim RPP 
Dim PageNo 
I=1 
RPP=50 
PageNo=CInt(Request("PageNo")) 
’上面即是新插入的, 
Dim Recordset1 
Dim Recordset1_numRows 
Set Recordset1 = Server.CreateObject("ADODB.Recordset") 
Recordset1.ActiveConnection = MM_数据库名_STRING 
Recordset1.Source = "SELECT * FROM 数据库名" 
Recordset1.CursorType = 1 ’将上面代码的0改为1. 
Recordset1.CursorLocation = 2 
Recordset1.LockType = 1 
Recordset1.Open() 
Recordset1_numRows = 0 ’再在此行的下一行开始加入如下代码: 
Recordset1.PageSize=RPP 
If PageNo<=0 Then PageNo=1 
If PageNo>Recordset1.PageCount Then PageNo=Recordset1.PageCount 
Recordset1.AbsolutePage=PageNo 
Sub ShowPageInfo(tPageCount,cPageNo) 
Response.Write "第"&cPageNo&"页[共"&tPageCount&"页]" 
End Sub 
Sub ShowPageNavi(tPageCount,cPageNo) 
If cPageNo<1 Then cPageNo=1 
If tPageCount<1 Then tPageCount=1 
If cPageNo>tPageCount Then cPageNo=tPageCount 
Dim NaviLength 
NaviLength=10 ’NaviLength:显示的数字链接个数 
Dim I,StartPage,EndPage 
StartPage=(cPageNoNaviLength)*NaviLength+1 
If (cPageNo Mod NaviLength)=0 Then StartPage=StartPage-NaviLength 
EndPage=StartPage+NaviLength-1 
If EndPage>tPageCount Then EndPage=tPageCount 
If StartPage>1 Then 
Response.Write "<a class=""pageNavi"" href=""?PageNo=" & (cPageNo-NaviLength) & """><<</a> " 
Else 
Response.Write "<font color=""#CCCCCC""><<</font> " 
End If 
For I=StartPage To EndPage 
If I=cPageNo Then 
Response.Write "<b>"&I&"</b>" 
Else 
Response.Write "<a class=""pageNavi"" href=""?PageNo=" & I & """>" & I & "</a>" 
End If 
If I<>tPageCount Then Response.Write "&nbsp;" 
Next 
If EndPage<tPageCount Then 
Response.Write " <a class=""pageNavi"" href=""?PageNo=" & (cPageNo+NaviLength) & """>>></a>" 
Else 
Response.Write " <font color=""#CCCCCC"">>></font> " 
End If 
End Sub 
%> 

上面代码中:RPP:指定每页显示的记录条数。即每页显示几条数据。

  NaviLength:显示的数字链接个数,即10就为1 2 3 ...10的连接个数。

  若要显示所有连接的页(个)数,你可以设置为:NaviLength=tPageCount。

  这时代码已经差不多了,但还要在显示的地方(如表格)中加点代码才行吧,(要不然怎么显示,呵~~~)如我们插入一个2行3列的表格。

  1.将光标移在第一行第一列中,切换到代码中加入:<%=(PageNo-1)*RPP+I%>

  这个代码是显示序号用的。

  2.右边2个单元格(当然你自己可以根据需要分更多的列)就是为你要显示的记录了。请分别从绑定的记录集中选中你要显示的字段拖放在相应的单元格中,(也可以选中后再点右下角的“插入”按钮)。这里我们就先拖2个进来如“编号”和“公司名称”。分别到1行第2个单元格和1行第3个单元格中。

  3.这个是个关键的,请将光标移到第一行任意单元格中,再来点选窗口底下的<tr>,这时你看看代码,<tr>....</tr>就被选中了。这时请在<tr>....</tr>的前面插入如下代码:

以下是引用片段:
<% 
If Recordset1.EOF OR Recordset1.BOF Then 
Else 
For I=1 To RPP 
%>再在<tr>....</tr>之后插入如下代码: 
<% 
Recordset1.MoveNext 
If Recordset1.EOF OR Recordset1.BOF Then Exit For 
Next 
End If 
%> 


  4.这是就完成表格的第一行的工作。下来也是关键,即分页的连接。光标在第2行第一个单元格中时在代码窗口插入:

<% showPageInfo Recordset1.PageCount,PageNo %> 的代码。右边的2个单元格将其合并,在代码中插入:

<% showPageNavi Recordset1.PageCount,PageNo %>

  的代码。

  5.大功告成!这时感快预览一下吧。。。。

  表格的全部代码如下:

以下是引用片段:
<table width="710" border="0" align="center" cellpadding="3" cellspacing="1" bgcolor="#333333"> 
<% 
If Recordset1.EOF OR Recordset1.BOF Then 
Else 
For I=1 To RPP 
%> 
<tr bgcolor="#FFFFFF"> 
<td width="30" align="center"><%=(PageNo-1)*RPP+I%></td> 
<td><%=(Recordset1.Fields.Item("编号").Value)%></td> 
<td><%=(Recordset1.Fields.Item("公司名称").Value)%></td> 
</tr> 
<% 
Recordset1.MoveNext 
If Recordset1.EOF OR Recordset1.BOF Then Exit For 
Next 
End If 
%> 
<tr bgcolor="#FFFFFF"> 
<td colspan="3"><table width="100%" border="0" cellspacing="0" cellpadding="2"> 
<tr bgcolor="#006699" class="w12"> 
<td width="121" align="center"><% showPageInfo Recordset1.PageCount,PageNo %> 
</td> 
<td width="573" align="center"> 
<% showPageNavi Recordset1.PageCount,PageNo %> 
</td> 
</tr> 
</table></td> 
</tr> 
</table> 

  这时你去点应用程序中的“服务器行为”中的记录集,在代码中就显示为一下代码,也是我的原代码:

以下是引用片段:
<% 
Dim I 
Dim RPP’RPP:指定每页显示的记录条数, 
Dim PageNo 
I=1 
RPP=50 
PageNo=CInt(Request("PageNo")) 
Dim Recordset1 
Dim Recordset1_numRows 
Set Recordset1 = Server.CreateObject("ADODB.Recordset") 
Recordset1.ActiveConnection = MM_数据库名_STRING 
Recordset1.Source = "SELECT * FROM 表名 ORDER BY 编号 ASC" 
Recordset1.CursorType = 1 
Recordset1.CursorLocation = 2 
Recordset1.LockType = 1 
Recordset1.Open() 
Recordset1_numRows = 0 
Recordset1.PageSize=RPP 
If PageNo<=0 Then PageNo=1 
If PageNo>Recordset1.PageCount Then PageNo=Recordset1.PageCount 
Recordset1.AbsolutePage=PageNo 
Sub ShowPageInfo(tPageCount,cPageNo) 
Response.Write "第"&cPageNo&"页[共"&tPageCount&"页]" 
End Sub 
Sub ShowPageNavi(tPageCount,cPageNo) 
If cPageNo<1 Then cPageNo=1 
If tPageCount<1 Then tPageCount=1 
If cPageNo>tPageCount Then cPageNo=tPageCount 
Dim NaviLength 
NaviLength=20 ’NaviLength:显示的数字链接个数 
Dim I,StartPage,EndPage 
StartPage=(cPageNoNaviLength)*NaviLength+1 
If (cPageNo Mod NaviLength)=0 Then StartPage=StartPage-NaviLength 
EndPage=StartPage+NaviLength-1 
If EndPage>tPageCount Then EndPage=tPageCount 
If StartPage>1 Then 
Response.Write "<a class=""pageNavi"" href=""?PageNo=" & (cPageNo-NaviLength) & """><<</a> " 
Else 
Response.Write "<font color=""#CCCCCC""><<</font> " 
End If 
For I=StartPage To EndPage 
If I=cPageNo Then 
Response.Write "<b>"&I&"</b>" 
Else 
Response.Write "<a class=""pageNavi"" href=""?PageNo=" & I & """>" & I & "</a>" 
End If 
If I<>tPageCount Then Response.Write "&nbsp;" 
Next 
If EndPage<tPageCount Then 
Response.Write " <a class=""pageNavi"" href=""?PageNo=" & (cPageNo+NaviLength) & """>>></a>" 
Else 
Response.Write " <font color=""#CCCCCC"">>></font> " 
End If 
End Sub 
%> 

不过有一个缺点就是:如当你想找99页时点>>9次,要是有一个输入框,输入99后回车就到99就完美了。

ASP中如何过滤HTML标签

分类:ASP  来源:网络  时间:2011-3-12 12:07:44

正常ASP中对录入内容的过滤仅仅是对左尖括号(<)和右尖括号的(>)的HTML源码的替换,以在页面中显示为左右尖括号,而不是将尖括号作为HTML标签执行了。

当然这应该是属于正常过滤手法,而还有一种过滤方法则是将一对尖括号及尖括号中的所有字符均替换不显示,作为过滤HTML标签的最终极手法,但该方法对于内容中必须描述有关尖括号内容则是过滤过头了(比如本文起始所引用的相关尖括号符号均将看不见“<>”,文章中用来描述HTML标签“<hr>”也将看不见)。

不过,总归是有需要将所有尖括号中内容全部替换的时候。很显然是需要进行正则的。搜索了一段代码


Function nohtml(str)
dim re
Set re=new RegExp
re.IgnoreCase =true
re.Global=True
re.Pattern="(<.[^<]*>)"
str=re.replace(str,"")
re.Pattern="(</[^<]*>)"
str=re.replace(str,"")
nohtml=str
set re=nothing
End Function

 

或者


Function nohtml(str)
dim re
Set re=new RegExp
re.IgnoreCase =true
re.Global=True
re.Pattern="<(.[^>]*)>"
str=re.replace(str,"")
nohtml=str
set re=nothing
End Function

 

 

ASP日期相关处理方法

分类:Asp  来源:网络  时间:2010-11-20 14:37:28

FormatDateTime 函数
返回表达式,此表达式已被格式化为日期或时间。
FormatDateTime(Date[, NamedFormat])
参数
Date
必选项。要被格式化的日期表达式。
NamedFormat
可选项。指示所使用的日期/时间格式的数值,如果省略,则使用 vbGeneralDate。
设置
NamedFormat 参数可以有以下值:
常数 值 描述
vbGeneralDate 0 显示日期和/或时间。如果有日期部分,则将该部分显示为短日期格式。如果有时间部分,则将该部分显示为长时间格式。如果都存在,则显示所有部分。
vbLongDate 1 使用计算机区域设置中指定的长日期格式显示日期。
vbShortDate 2 使用计算机区域设置中指定的短日期格式显示日期。
vbLongTime 3 使用计算机区域设置中指定的时间格式显示时间。
vbShortTime 4 使用 24 小时格式 (hh:mm) 显示时间。
说明
下面例子利用 FormatDateTime 函数把表达式格式化为长日期型并且把它赋给 MyDateTime:
Function GetCurrentDate
‘FormatDateTime 把日期型格式化为长日期型。
GetCurrentDate = FormatDateTime(Date, 1)
End Function

ASP中怎样获取单选框的值

分类:Asp  来源:网络  时间:2010-11-20 14:36:25

<html>
<head><title>sex</title></head>
<%
strsex=”"
if request(”radsex”)=”M” then
strsex=”帅哥”
elseif request(”radsex”)=”F” then
strsex=”美女”
end if
if request(”radedu”)=”1″ then edu=”初中”
if request(”radedu”)=”2″ then edu=”高中”
if request(”radedu”)=”3″ then edu=”大学”
if request(”radedu”)=”4″ then edu=”硕士”
response.write “这位” &strsex &”的学历是” &edu
%>
<form method=post action=”sex.asp”>
<fieldset>
<legend>性别</legend>
<input type=radio name=radsex value=”M” checked>男</br>
<input type=radio name=radsex value=”F”>女</br>
</fieldset>
<fieldset>
<legend>学历</legend>
<input type=radio name=radedu value=1 checked>初中</br>
<input type=radio name=radedu value=2>高中</br>
<input type=radio name=radedu value=3>大学</br>
<input type=radio name=radedu value=4>硕士</br>
</fieldset>
<p></p>
<input type=submit name=btnok value=确定>
</form>
</body>
</html>

ASP无组件上传后乱码问题

分类:Asp  来源:网络  时间:2010-11-20 14:35:13

第一步:修改 连接数据库文件
<%@LANGUAGE=”VBSCRIPT” CODEPAGE=”936″%>
<%
Option Explicit
Response.Buffer = True
Server.ScriptTimeOut = 90
Session.CodePage=936
%>
参考的是:
如果制作的网页脚本与WEB服务端的默认代码页不同,则必须指明代码页:
codepage=936 简体中文GBK
codepage=950 繁体中文BIG5
codepage=437 美国/加拿大英语
codepage=932 日文
codepage=949 韩文
codepage=866 俄文
codepage=65001 unicode UFT-8

第二步:把所有的
<meta http-equiv=”Content-Type” content=”text/html; charset=UTF-8″ />
改成
<meta http-equiv=”Content-Type” content=”text/html; charset=GB2312″ />
再另存为ANSI
==========================================

GB2312转换为UTF-8编码的方法

第一步:
修改连接数据库文件
<%@LANGUAGE=”VBSCRIPT” CODEPAGE=”65001″%>
<%
Option Explicit
Response.Buffer=True
Session.CodePage=65001
Server.ScriptTimeOut = 90
%>
然后修改页面,将程序代码:
<meta http-equiv=”Content-Type” content=”text/html; charset=GB2312″ />
修改为:
程序代码:
<meta http-equiv=”Content-Type” content=”text/html; charset=UTF-8″ />

第二步:
将所有ASP和JS文件,用记事本打开,然后另存为,编码本来是默认, 选择成UTF-8就可以了

第三步:
插入insert into content(a1,a2,a3) values(N’ “&a1&”‘,N’ “&a2&”‘)

asp获取浏览器类型及操作系统版本

分类:Asp  来源:网络  时间:2010-11-20 14:33:45

Function SystemCheck()
useragent=Request.ServerVariables(”HTTP_USER_AGENT”)
‘转化为小写
useragent=Lcase(useragent)

‘Browser Check
dim btype,bname,bversion,idx,Os
btype=”"
if inStr(useragent,”icab”) then
bname=”iCab”
elseif inStr(useragent,”lynx”) then
bname=”Lynx”
elseif inStr(useragent,”links”) then
bname=”Links”
elseif inStr(useragent,”elinks”) then
bname=”ELinks”
elseif inStr(useragent,”jbrowser”) then
bname=”JBrowser”
elseif inStr(useragent,”konqueror”) then
bname=”Konqueror”
elseif inStr(useragent,”gecko”) then
bname=”Mozilla”
btype=btype &”[Gecko]”
if inStr(useragent,”aol”) then
bname=”AOL”
elseif inStr(useragent,”netscape”) then
bname=”Netscape”
elseif inStr(useragent,”firefox”) then
bname=”FireFox”
elseif inStr(useragent,”chimera”) then
bname=”Chimera”
elseif inStr(useragent,”camino”) then
bname=”Camino”
elseif inStr(useragent,”galeon”) then
bname=”Galeon”
elseif inStr(useragent,”k-meleon”) then
bname=”K-Meleon”
end if
elseif inStr(useragent,”bot”) or inStr(useragent,”crawl”) or inStr(useragent,”spider”) or inStr(useragent,”mediapartners”) or inStr(useragent,”slurp”) then
btype=btype &”[Bot/Crawler/Spider]”
if inStr(useragent,”grub”) then
bname=”Grub”
elseif inStr(useragent,”googlebot”) or inStr(useragent,”google”) then
bname=”GoogleBot”
elseif inStr(useragent,”baidu”) then
bname=”BaiduBot”
elseif inStr(useragent,”sogou”) then
bname=”SogouBot”
elseif inStr(useragent,”msnbot”) then
bname=”MSN Bot”
elseif inStr(useragent,”slurp”) then
bname=”Yahoo! Slurp”
end if
elseif inStr(useragent,”wget”) then
bname=”Wget”
elseif inStr(useragent,”ask jeeves”) or inStr(useragent,”teoma”) then
bname=”Ask Jeeves/Teoma”
elseif inStr(useragent,”msie”) then
btype=”[IE"
bversion=Mid(useragent,inStr(useragent,"msie")+5,3)
btype=btype & bversion &"]”
bname=”IE”
if inStr(useragent,”msn”) then
bname=”MSN”
elseif inStr(useragent,”aol”) then
bname=”AOL”
elseif inStr(useragent,”webtv”) then
bname=”WebTV”
elseif inStr(useragent,”myie2″) then
bname=”MyIE2″
elseif inStr(useragent,”maxthon”) then
bname=”Maxthon”
elseif inStr(useragent,”gosurf”) then
bname=”GoSurf”
elseif inStr(useragent,”netcaptor”) then
bname=”NetCaptor”
elseif inStr(useragent,”sleipnir”) then
bname=”Sleipnir”
elseif inStr(useragent,”avant browser”) then
bname=”AvantBrowser”
elseif inStr(useragent,”greenbrowser”) then
bname=”GreenBrowser”
elseif inStr(useragent,”slimbrowser”) then
bname=”SlimBrowser”
end if
elseif inStr(useragent,”opera”) then
bname=”Opera”
idx=inStr(useragent,”opera”)
bversion=mid(useragent,idx+6,idx+9)
bname=bname & bversion
elseif inStr(useragent,”applewebkit”) then
btype=”[AppleWebKit]”
if inStr(useragent,”omniweb”) then
bname=”OmniWeb”
elseif inStr(useragent,”safari”) then
bname=”Safari”
elseif inStr(useragent,”mozilla”) then
bname=”Mozilla”
end if
end if

‘Os Check
if inStr(useragent,”windows ce”) then
Os=”Windows ce”
elseif inStr(useragent,”windows 95″) then
Os=”Windows 95″
elseif inStr(useragent,”windows 98″) then
Os=”Windows 98″
elseif inStr(useragent,”windows 2000″) then
Os=”Windows 2000″
elseif inStr(useragent,”windows xp”) then
Os=”Windows XP”
elseif inStr(useragent,”windows nt 5.0″) then
Os=”Windows 2000″
elseif inStr(useragent,”windows nt 5.1″) then
Os=”Windows XP”
elseif inStr(useragent,”windows nt 5.2″) then
Os=”Windows 2003″
elseif inStr(useragent,”windows nt”) then
Os=”Windows NT”
elseif inStr(useragent,”win32″) then
Os=”Win32″
elseif inStr(useragent,”x11″) or inStr(useragent,”unix”) then
Os=”unix”
elseif inStr(useragent,”sunos”) or inStr(useragent,”sun os”) then
Os=”SUN OS”
elseif inStr(useragent,”powerpc”) or inStr(useragent,”ppc”) then
Os=”PowerPC”
elseif inStr(useragent,”macintosh”) then
Os=”Mac”
elseif inStr(useragent,”mac osx”) then
Os=”MacOSX”
elseif inStr(useragent,”freebsd”) then
Os=”FreeBSD”
elseif inStr(useragent,”linux”) then
Os=”Linux”
elseif inStr(useragent,”palmsource”) or inStr(useragent,”palmos”) then
Os=”PalmOS”
elseif inStr(useragent,”wap “) then
Os=”WAP”
end if

if bname=”" then bname=”unknow”
if Os=”" then Os=”unknow”

SystemCheck=bname & btype & “/” &Os
end Function
 

asp中修补upfile.asp上传漏洞

分类:Asp  来源:网络  时间:2010-11-20 14:32:35

ASP文件相关的一些函数。有以下几个:
1. 得到文件扩展名
2. ASP上传文件漏洞检测
3. 格式化显示文件大小
4. asp检测上传图片是否为真实图片
5. 上传文件扩展名检测
6. 取得文件对应的图标
7. 下载文件等相关函数

程序代码:
<%
‘*******************************************************
‘作    用: 得到文件扩展名
‘函数名: GetFileExt(fileTrue)
‘参    数: sFileName 文件名
‘返回值: 返回文件扩展名
‘*******************************************************
function GetFileExt(sFileName)
GetFileExt = UCase(Mid(sFileName,InStrRev (sFileName, “.”)+1))      ‘下面有附
End function

‘*******************************************************
‘作    用: ASP上传漏洞 “″ 防范
‘函数名: TrueStr(fileTrue)
‘参    数: sFileName 文件名
‘返回值: 合法文件返回 True ,否则返回False
‘*******************************************************
function IsTrueFileName(sFileName)
dim str_len,pos
str_len=len(sFileName)
pos=Instr(sFileName,chr(0))
If pos=0 or pos=str_len then
IsTrueFileName = true
else
IsTrueFileName = false
End If
End function

‘*******************************************************
‘作    用: 检测上传的图片文件(jpeg,gif,bmp,png)是否真的为图片
‘函数名: TrueStr(fileTrue)
‘参    数: sFileName 文件名(此处文件名是文件夹的物理全路径)
‘返回值: 确实为图片文件则返回 True ,否则返回False
‘*******************************************************
function IsImgFile(sFileName)
const adTypeBinary=1
dim return
dim jpg(1):jpg(0)=CByte(&HFF):jpg(1)=CByte(&HD8)
dim bmp(1):bmp(0)=CByte(&H42):bmp(1)=CByte(&H4D)
dim png(3):png(0)=CByte(&H89):png(1)=CByte(&H50):png(2)=CByte(&H4E):png(3)=CByte(&H47)
dim gif(5):gif(0)=CByte(&H47):gif(1)=CByte(&H49):gif(2)=CByte(&H46):gif(3)=CByte(&H39):gif(4)=CByte(&H38):gif(5)=CByte(&H61)

on error resume next

return=false
dim fstream,fileExt,stamp,i
‘得到文件后缀并转化为小写
FileExt = LCase(GetFileExt(sFileName))
‘如果文件后缀为 jpg,jpeg,bmp,gif,png 中的任一种
‘则执行真实图片判断
If strInString(FileExt,”jpg|jpeg|bmp|gif|png”)=true then
Set fstream=Server.createobject(”ADODB.Stream”)
fstream.Open
fstream.Type=adTypeBinary
fstream.LoadFromFile sFileName
fstream.position=0
select case LCase(FileExt)
case “jpg”,”jpeg”
stamp=fstream.read(2)
for i=0 to 1
If ascB(MidB(stamp,i+1,1))=jpg(i) then return=true else return=false
next
case “gif”
stamp=fstream.read(6)
for i=0 to 5
If ascB(MidB(stamp,i+1,1))=gif(i) then return=true else return=false
next
case “png”
stamp=fstream.read(4)
for i=0 to 3
If ascB(MidB(stamp,i+1,1))=png(i) then return=true else return=false
next
case “bmp”
stamp=fstream.read(2)
for i=0 to 1
If ascB(MidB(stamp,i+1,1))=bmp(i) then return=true else return=false
next
End select

fstream.Close
Set fseteam=nothing
If err.number<>0 then return = false
else
return = true
End If
IsImgFile = return
End function

‘*******************************************************
‘作    用: 上传文件扩展名检测
‘函数名: CheckFileExt
‘参    数: sFileExt 上传文件夹的后缀
‘          strExt     允许或禁止上传文件夹的后缀,多个以”|”分隔
‘          blnAllow 是允许还是禁止上传 strExt 中指定的后缀
‘返回值: 合法文件返回 True ,否则返回False
‘*******************************************************
Function CheckFileExt(sFileExt,strExt,blnAllow)
dim arrExt,return
‘= 禁止上传的文件列表
’strExt = “EXE|JS|BAT|HTML|HTM|COM|ASP|ASA|DLL|PHP|JSP|CGI”
sFileExt = UCase(sFileExt)
strExt     = UCase(strExt)
arrExt = split(strExt,”|”)
If blnAllow=true then           ‘只允许上传指定的文件
return = false
for i=0 to UBound(arrExt)
If sFileExt=arrExt(i) then return=true
next
‘response.write “Ext: “&sFileExt & ” return: ” & return & “     ”
else                          ‘禁止上传指定的文件
return = true
for i=0 to UBound(arrExt)
If sFileExt=arrExt(i) then return=false
next
End If
CheckFileExt = return
End Function

‘*******************************************************
‘作    用: 格式化显示文件大小
‘FileSize: 文件大小
‘*******************************************************
Function FormatSize(FileSize)
If FileSize<1024 then FormatSize = FileSize & ” Byte”
If FileSize/1024 <1024 And FileSize/1024 > 1 then
FileSize = FileSize/1024
FormatSize=round(FileSize*100)/100 & ” KB”
Elseif FileSize/(1024*1024) > 1 Then
FileSize = FileSize/(1024*1024)
FormatSize = round(FileSize*100)/100 & ” MB”
End If
End function

‘*******************************************************
‘作    用: 取得文件对应的图标
‘函数名: FormatSize(FileSize)
‘参    数: FileName 文件名
‘返回值: 合法文件返回 True ,否则返回False
‘*******************************************************
Function GetFileIcon(FileName)
dim FileExt,Fso1
FileExt = GetFileExt(FileName)
ImgPath= strAdminRoot &    “Style/images/file/” & FileExt & “.gif”
Set Fso1 = Server.CreateObject(”Scripting.FileSystemObject”)
If Fso1.fileExists(server.MapPath(ImgPath))=true then
GetFileIcon= “<img src=”"” & ImgPath & “”">”
else
GetFileIcon= “<img src=”"” & strAdminRoot & “Style/images/file/unknow.gif” & “”">”
End If
Set Fso1=nothing
End Function
‘*******************************************************
‘作用:下载文件。
‘函数名: DownFile(FileName)
‘ FileName
‘*******************************************************
Sub DownFile(FileName)
fname = server.MapPath(fname)
filename=split(fname,””)

Set objAdoStream=Server.createObject(”ADODB.Stream”)
objAdoStream.Type=1
objAdoStream.open()
objAdoStream.LoadFromFile(fname)
strchar=objAdoStream.Read()
fsize=objAdoStream.size
objAdoStream.Close()
Set objAdoStream=nothing

Response.AddHeader “content-type”,”application/x-msdownload”
response.AddHeader “Content-Disposition”,”attachment;filename=” & filename(ubound(filename))
Response.AddHeader “content-length”, fsize

Response.BinaryWrite(strchar)
Response.Flush()
End Sub
%>
——————————————————————————————————————————
附1:
Mid 函数
返回 Variant (String),其中包含字符串中指定数量的字符。
语法
Mid(string, start[, length])
start 必要参数。为 Long。string 中被取出部分的字符位置。如果 start 超过 string 的字符数,Mid 返回零长度字符串 (”")。
length 可选参数;为 Variant (Long)。要返回的字符数。如果省略或 length 超过文本的字符数(包括 start 处的字符),将返回字符串中从 start 到尾端的所有字符。
有可能是你的start变量值大于字符串长度,导致返回空字符串,或者是变量值为0,导致错误
附2:
InstrRev的用法

描述
返回某字符串在另一个字符串中出现的从结尾计起的位置。
语法
InstrRev(string1, string2[, start[, compare]])
InstrRev 函数的语法有以下参数:
参数 描述
string1 必选。接受搜索的字符串表达式。
string2 必选。被搜索的字符串表达式。
start 可选。数值表达式,用于设置每次搜索的开始位置。如果省略,则默认值为 -1,表示从最后一个字符的位置开始搜索。如果 start 包含 Null,则出现错误。
compare 可选。在计算子字符串时,指示要使用的比较类型的数值。如果省略,将执行二进制比较。有关数值,请参阅“设置”部分。

设置
compare 参数可以有以下值:
常数 Value 描述
vbBinaryCompare 0 执行二进制比较。
vbTextCompare 1 执行文本比较。
vbDatabaseCompare 2 执行基于包含在数据库(在此数据库中执行比较)中的信息的比较。

返回值
InStrRev 返回以下值:
如果 InStrRev 返回
string1 为零长度 0
string1 为 Null Null
string2 为零长度 start
string2 为 Null Null
string2 没有找到 0
在 string1 中找到 string2 找到匹配字符串的位置
start > Len(string2) 0

说明
请注意 InstrRev 函数的语法不同于 Instr 函数的语法。

 

ASP中页面另存为EXCEL的方法

分类:Asp  来源:网络  时间:2010-11-20 14:30:42

<% Response.Buffer  =  TRUE
Response.AddHeader “Content-Disposition”,”attachment:filename=查询报表”&replace(now(),”:”, “-”)& “rxx “”.xls”
Response.contentType= “application/vnd.ms-excel”
%>

当页面存在数字时,导出EXCEL就变为科学记数法的形式,要使数字正常显示,请尝试下面办法。

“首先,我们了解一下excel从web页面上导出的原理。当我们把这些数据发送到客户端时,我们想让客户端程序(浏览器)以excel的格式读取它,所以把mime类型设为:application/vnd.ms-excel,当excel读取文件时会以每个cell的格式呈现数据,如果cell没有规定的格式,则excel会以默认的格式去呈现该cell的数据。这样就给我们提供了自定义数据格式的空间,当然我们必须使用excel支持的格式。下面就列出常用的一些格式:
1) 文本:vnd.ms-excel.numberformat:@
2) 日期:vnd.ms-excel.numberformat:yyyy/mm/dd
3) 数字:vnd.ms-excel.numberformat:#,##0.00
4) 货币:vnd.ms-excel.numberformat:¥#,##0.00
5) 百分比:vnd.ms-excel.numberformat: #0.00%
这些格式你也可以自定义,比如年月你可以定义为:yy-mm等等。那么知道了这些格式,怎么去把这些格式添加到cell中呢?很简单,我们只需要把样式添加到对应的标签对(即闭合标签)即可。如<td></td>,给标签对<td></td>添加样式,如下: <td style=”vnd.ms-excel.numberformat:@”>410522198402161833</td>
同样,我们也可以给<div></div>添加样式,也可以给<tr>< /tr>,<table></table>添加样式;当我们在父标签对和子标签对都添加样式时,数据会以哪一个样式呈现呢?经过测试,会以离数据最近的样式呈现.

 

ASP初学者常犯的几个错误

分类:Asp  来源:网络  时间:2010-10-21 22:50:17

1.记录集关闭之前再次打开:
------------------------------------
sql="select * from test"
rs.open sql,conn,1,1
if not rs.eof then
dim myName
myName=rs("name")
end if
sql="select * from myBook"
rs.open sql,conn,1,1
-------------------------------------
解决:在第二次rs.open之前先关闭 rs.close

set rs1=server.createobject
rs1.open sql,conn,1,1

2,用SQL关键字做表名或字段名
-------------------------------------
sql="select * from user"
rs.open sql,conn,1,1
-------------------------------------
user为sql关键字
解决:改为
sql="select * from [user]"


3,用锁定方式去进行update
-------------------------------------
sql="select * from [user]"
rs.open sql,conn,1,1
rs.addnew

rs("userName")="aa"
rs.update
-------------------------------------
当前记录集的打开方式为只读
解决:
改为
rs.open sql,conn,1,3

4,在查询语句中采用的对比字段值与字段类型不符 本文首发OK124
-----------------------------------------
sql="select * from [user] where id= " %26 myID %26 " "
rs.open sql,conn,1,1
-----------------------------------------
假设表中设计ID为数字型,那么些时出错。
解决:
sql="select * from [user] where id=" %26 myID

5,未检查变量值而出错
-----------------------------------------
sql="select * from [user] where id=" %26 myID
rs.open sql,conn,1,1
-----------------------------------------
假设myID变量此时值为null,那么sql将成为
sql="select * from [user] where id="
解决:
在前面加上
if isnull(myID) then 出错提示

6,未检查变量值类型而出错
-----------------------------------------
sql="select * from [user] where id=" %26 myID
rs.open sql,conn,1,1
-----------------------------------------
假设id为数字型,myID变量此时值不为null,但为字符,比如myID此时为"aa"
那么sql将成为
sql="select * from [user] where id=aa"
解决:
在前面加上
if isnumeric(myID)=false then 出错提示
网+络时空 www ok124 com


这也可以有效防止 sql injection 漏洞攻击。

7,由于数据库文件所在目录的NTFS权限而引起的 不能更新。数据库或对象为只读"错误。
说明:
WIN2K系统延续了WINNT系统的NTFS权限。
对于系统中的文夹都有默认的安全设置。
而通过HTTP对WWW访问时的系统默认用户是 iusr_计算机名 用户 ,它属于guest组。
当通过HTTP访问时,可以ASP或JSP,也或是PHP或.NET程序对数据进行修改操作:
比如:
当打开某一个文章时,程序设定,文章的阅读次数=原阅读次数+1
执行
conn.execute("update arts set clicks=clicks+1 where id=n")
语句时,如果 iusr_计算机名 用户没有对数据库的写权限时,就会出错.
解决方法:
找到数据库所在目录
右键》属性》安全选项卡》设置 iusr_计算机名 用户的写权限(当然,也可以是everyone)

asp编程中VBscript和javascript的选择

分类:Asp  来源:网络  时间:2010-9-23 16:06:49

服务器端:

大家知道ASP支持这两中脚本语言,也就是在服务器端用哪个都行,但大部分人在服务器

端用VBscript比较多,对于新手来说一般用VBscript就能了。

客户端:

目前流行的主要是微软和网景这两大浏览器,微软的IE对VBscript和JAVAscript都支持,

而网景的浏览器却不支持VBscript,为了兼容我们应该在客户端使用JAVAscript脚本语言,

从另方面由于JAVAscript有十分强大的交互性,使的我们在客户端用他能实现许多复杂的

功能。

所以说,由于是对于新手,在服务器端使用VBscript,在客户端应该使用JAVAscript,

如果你实在不熟悉JAVAscript,你能找一些代码只要能实现其功能就能了,等有一定

基础后,在回头再去学习他。