您现在的位置: 365建站网 > 365文章 > 2.5  常用的ASP技巧与模块

2.5  常用的ASP技巧与模块

文章来源:365jz.com     点击数:367    更新时间:2009-07-15 14:42   参与评论

2.5  常用的ASP技巧与模块

从软件开发角度讲,代码重用是快速开发的一个重要思想。下面介绍一些ASP开发中常用的通用模块与组件,目的不是教会大家这些模块或组件的设计或实现原理,而只是向大家说明这种模块或组件的存在性,重点介绍它们的使用方法。当自己在开发过程中需要实现某种功能时,可以从本节所介绍的内容中找到答案,这就达到了本节讲解的目的。

2.5.1  通用的分页模块

分页是在ASP中经常遇到的一个问题,而且也算一个不小的难题,尽管RecordSet已经为我们提供了完善的分页机制,但往往我们还需要做不少工作。下面是笔者在参考前人工作的基础上自己编写的一个分页函数,已经在许多系统中使用,运行完全正常。读者可以在此基础上修改,设计出有自己风格的分页模块。实现代码参见例程2-7。

例程2-7  通用的分页函数ShowPage

<%

'***********************************************

'过程名:ShowPage

'作  用:显示“上一页 下一页”等信息

'参  数:sDesURL——链接地址,可以是一个文件名,也可以是一个有一些参数的URL

'       nTotalNumber——总数量

'       nMaxPerPage——每页数量

'           nCurrentPage——当前页

'       bShowTotal ——是否显示总数量

'       bShowCombo——是否用下拉列表显示所有页面以供跳转

'       sUnit——计数单位

'***********************************************

sub ShowPage(sDesURL, nTotalNumber, nMaxPerPage, nCurrentPage, bShowTotal,

bShowCombo, sUnit)

         dim n, i,strTemp,strUrl

         '计算页数

         if nTotalNumber mod nMaxPerPage=0 then

    n= nTotalNumber \ nMaxPerPage

       else

    n= nTotalNumber \ nMaxPerPage+1

       end if

       '判断nCurrentPage

       if nCurrentPage < 1 then

                nCurrentPage = 1

       elseif nCurrentPage > n then

                nCurrentPage = n

       endif

      

       strTemp= "<table align='center' ID="Table1"><form name='ShowPages' method='Post'

       action='" & sDesURL & "' ID="Form1"><tr><td>"

         if bShowTotal=true then

                  strTemp=strTemp & "共 <b>" & nTotalNumber & "</b> " & sUnit & "&nbsp;&nbsp;"

         end if

         '根据输入的sDesURL向它加入“?”或“&”

         strUrl=PasteURL(sDesURL)

       if nCurrentPage<2 then

              strTemp=strTemp & "首页 上一页&nbsp;"

       else

              strTemp=strTemp & "<a href='" & strUrl & "page=1'>首页</a>&nbsp;"

              strTemp=strTemp & "<a href='" & strUrl & "page=" & (nCurrentPage-1) & "'>

              上一页</a>&nbsp;"

       end if

       if n-nCurrentPage<1 then

              strTemp=strTemp & "下一页 尾页"

       else

              strTemp=strTemp & "<a href='" & strUrl & "page=" & (nCurrentPage+1) & "'>

                下一页</a>&nbsp;"

              strTemp=strTemp & "<a href='" & strUrl & "page=" & n & "'>尾页</a>"

       end if

               strTemp=strTemp & "&nbsp;页次:<strong><font color=red>" & nCurrentPage & "

               </font>/" & n & "</strong>页 "

    strTemp=strTemp & "&nbsp;<b>" & nMaxPerPage & "</b>" & sUnit & "/页"

         if bShowCombo=True then

                  strTemp=strTemp & "&nbsp;转到:<select name='page' size='1' onchange=

                            'javascript:submit()' ID="Select1">"  

    for i = 1 to n  

              strTemp=strTemp & "<option value='" & i & "'"

                            if cint(nCurrentPage)=cint(i) then

                            strTemp=strTemp & " selected "

                            strTemp=strTemp & ">第" & i & "页</option>"  

             next

                           strTemp=strTemp & "</select>"

         end if

         strTemp=strTemp & "</td></tr></form></table>"

         response.write strTemp

end sub

%>

在这个函数中用到了一个PasteURL函数,用来根据不同情况向输入的URL中加入“?”或“&”,作用很简单,但将它提出来后使得ShowPage函数变得很简明。例程2-8是PasteURL函数的实现。

例程2-8  PasteURL函数的实现

'***********************************************

'函数名:PasteURL

'作  用:向地址中加入“?”或“&”

'参  数:strUrl——网址

'返回值:加了“?”或“&”的网址

'***********************************************

function PasteURL(strUrl)

         if strUrl="" then

                  PasteURL=""

                  exit function

         end if

         '如果传入的URL末尾不是“?”,有两种情况:

         '1.无“?”,此时需加入一个“?”

         '2.有“?”,再判断有无“&”

         if InStr(strUrl,"?")<len(strUrl) then

                  if InStr(strUrl,"?")>1 then

                            if InStr(strUrl,"&")<len(strUrl) then

                                     PasteURL=strUrl & "&"

                            else

                                     PasteURL=strUrl

                            end if

                  else

                            PasteURL=strUrl & "?"

                  end if

         else

                  PasteURL=strUrl

         end if

end function

以上代码均在本书附带光盘中的showpage.asp文件中,在使用时将这个文件用include语句包含进来,无须做任何修改就可以使用ShowPage函数来实现分页功能。

[NextPage]

 

2.5.2  检测邮件地址合法性的函数

检测E-mail地址正确性也是在应用中常遇到的问题。下面给出一个IsValidEmail函数,用来检测输入的邮件格式是否正确,利用这样的原理,还可以自行编写检测电话号码、密码等格式正确性的函数。IsValidEmail函数的实现如例程2-9所示。

例程2-9  IsValidEmail函数的实现

<%

'********************************************

'函数名:IsValidEmail

'作  用:检查E-mail地址合法性

'参  数:sEmail——要检查的E-mail地址

'返回值:True——E-mail地址合法

'        False——E-mail地址不合法

'********************************************

function IsValidEmail(sEmail)

         dim names, name, i, c

         IsValidEmail = true

         names = Split(sEmail, "@")

         if UBound(names) <> 1 then

            IsValidEmail = false

            exit function

         end if

         for each name in names

                  if Len(name) <= 0 then

                            IsValidEmail = false

              exit function

                  end if

                  for i = 1 to Len(name)

                      c = Lcase(Mid(name, i, 1))

                            if InStr("abcdefghijklmnopqrstuvwxyz_-.", c) <= 0 and not IsNumeric(c) then

                         IsValidEmail = false

                         exit function

                       end if

            next

            if Left(name, 1) = "." or Right(name, 1) = "." then

      IsValidEmail = false

               exit function

            end if

         next

         if InStr(names(1), ".") <= 0 then

                  IsValidEmail = false

            exit function

         end if

         i = Len(names(1)) - InStrRev(names(1), ".")

         if i <> 2 and i <> 3 then

            IsValidEmail = false

            exit function

         end if

         if InStr(sEmail, "..") > 0 then

            IsValidEmail = false

         end if

end function

%>

此函数在本书附带光盘中的validate.asp文件内,略经修改,就可以将它用到客户端脚本中用来检测E-mail地址的合法性,前提是要在客户端脚本中用VBScript。

2.5.3  图片计数器的简单实现

计数器也是一个对自己网站评价分析的基本工具,实现一个图片计数器非常简单,但想做得很好却并不很容易。因为这涉及到多个客户端并发操作的问题。本节中将点击数存储在Application("nHits")和一个文本文件中,在每次需要显示计数器时,将它读出,并与Application("nHits")比较,如果读出的数比Application("nHits")小,则说明在刚才的读取过程中,已经有其他客户端将Application中的值修改。此时将Application("nHits")值加1,然后再写入文件。

事实上,有比这个更好的解决办法,那就是在Application_OnStart中读取文件,然后利用传统的方法改写Application("nHits")的值,接下来再将其写入到文本文件中。为了演示方便,这里列出前一种方法的代码,第二种方法需要用到global.asa文件,读者根据下面的代码稍加改动即可完成。实现计数增加的代码位于counter.asp文件中,如例程2-10所示。

例程2-10  实现计数增加的counter.asp文件代码

<TABLE cellSpacing=0 cellPadding=0  border=0 align=center>

  <TBODY>

  <TR>

    <TD width="100%" align=center><font color=#ffffff>您是第</font>

    <%

           dim strFile       '文件绝对路径

           dim fso            'FSO实例

           dim ftrFile       'FileStream实例

           dim nVisitors '访问者数目

           dim nCountLen '数字长度

           dim i

           'iomode 常量定义

           Const ForReading = 1, ForWriting = 2, ForAppending = 8    

      strFile=Server.MapPath("counter/hits.txt")

      Set fso=CreateObject("Scripting.FileSystemObject")

           '读文件,将点击数存储在Application中,以防止多用户时数据丢失

      Set ftrFile=fso.OpenTextFile(strFile, ForReading , True)

      nVisitors=ftrFile.readline

      if CLng(Application("nHits")) < nVisitors then

                  Application("nHits") = nVisitors + 1

           Else

                  Application("nHits") = Application("nHits") + 1

           End If

      ftrFile.close

      '写文件,写入Application("nHits")中的值,而不是nVisitors中的值,

      '这也是为了防止其他用户修改点击数而使写入的值不是最新的

      set ftrFile=fso.OpenTextFile(strFile, ForWriting , True)

      ftrFile.WriteLine(Application("nHits"))

      ftrFile.Close

     

      nCountLen=len(nVisitors)

      for i=1 to nCountLen

                  Response.Write "<img src=counter/3/"&mid(nVisitors,i,1)&".gif></img>"

      next

      set fso=nothing

    %><font color=#ffffff>位访问者</font>

   </TD>

  </TR>

  </TBODY>

</TABLE>

一个完整的计数器在本书附带光盘中的counter文件夹中,使用时只需要在需要显示访问量的地方用include语句将counter.asp包含进来即可。

如对本文有疑问,请提交到交流论坛,广大热心网友会为你解答!! 点击进入论坛

发表评论 (367人查看0条评论)
请自觉遵守互联网相关的政策法规,严禁发布色情、暴力、反动的言论。
昵称:
最新评论
------分隔线----------------------------

快速入口

· 365软件
· 杰创官网
· 建站工具
· 网站大全

其它栏目

· 建站教程
· 365学习

业务咨询

· 技术支持
· 服务时间:9:00-18:00
365建站网二维码

Powered by 365建站网 RSS地图 HTML地图

copyright © 2013-2024 版权所有 鄂ICP备17013400号