选择显示字体大小

在客户端执行数据库记录的分页显示

     绪论
  
  
    在asp程序应用中经常有一个任务就是在一个分页格式中显示数据库的查询结果。比如,当处理大量的记录内容时,应该一次只显示10个项目,这样就会使访问者使用这些信息更容易些。网络上已经有许多文章示范了多种对数据库查询结果进行分页的方法,其中较好的文章包括:
  
    使用存储过程数据库记录进行分页
  
    数据库分页样本代码
  
    使用getrows对记录进行分页
  
    但是所有这些文章介绍的方法都是在服务器端进行分页处理的。举例来说,假如有30条数据库记录,我们希望一次显示10条记录。当用户请求第一页数据时,asp页面就提取前10条记录并将它们发送给客户。当用户已经作好准备可以阅览下面10条记录时,他就点击一个链接,这时asp页面就被重新装载,通过查询字符串传递进来一个新的页面值,asp页面将取出第11条到20条记录,并将它们发给用户。
  
    本文中我们将把这整个处理过程移植到客户端。当用户联机查看数据时,这30个记录将全部以客户端javas cript 数组的形式发送到客户机上。另外,附加的客户端javas cript代码负责显示第1到第10条记录,并且链接到后面或者前面的记录集。当点击这些链接时,将执行客户端javas cript代码,新的数据会显示出来。由于这些都是在客户端发生的,因此只有在页面第一次被装载时才需要与web服务器联系,从而省却服务器与客户端间的数据传递消耗,无疑大大地提高了应用程序的性能。
  
    所需要的客户端脚本
  
    要实现在客户端显示分页数据库记录,asp页面应该生成什么样的客户端脚本呢?如同前面所述,我们需要创建一个客户端数组。因为我们准备在一个html表格中显示数据库记录,那么就要创建一个html数组,也许用它来显示一个html table行(<tr><td> database value </td></tr>)。这个数组中将包含所有我们希望允许用户翻阅的数据库元素。
  
    我们还需要一个客户端的javas cript函数以显示这些数组值的子集。使用这个函数,我们可以显示前n 个记录;当用户点击一个链接时,就可以显示后面(或前面) n个记录。也就是说,这个函数负责客户端分页
  
    最后,我们还需要一些方法在不更新页面的情况下来动态改变一个html页面的显示。这可以通过dhtml(动态html)来实现。要实现这个目的,我们需要在堆积所有输出的地方创建一个html div标记,然后使用客户端javas cript代码在这个div标记中动态修改内容。关于使用dhtml的更多信息,请阅读动态html入门及跨浏览器dhtml教程.
  
  创建一个分页
  
    要记住,这里举例的整个应用程序只包括一个asp页面。当这个asp页面被访问时,它将创建执行记录分页需要的所有客户端javas cript代码。为简化这个过程,我创建了一个vbs cript类来处理这个功能。使用这个类的时候,开发人员只需将他希望在访问者的web浏览器上进行分页的记录集传递进来即可。关于类的使用,请阅读在vbs cript中使用类。
  
    我将这个类命名为dhtmlgetrows,它包含两个属性和一个方法。两个属性是:
  
    1、 recsperpage:确定每一页显示多少个记录。
  
    2、 thstring:通过一个html表格显示这个进行了分页的结果;这个属性允许你为表格的标题指定一个字串。
  
    单一的方法是generatehtml(recordsetobject),它为分页应用程序返回完整的html:客户端javas cript代码和需要的div 标记。这个方法只需要一个参数recordsetobject,它应该是一个记录集对象,其中填充了你希望在一个分页格式中显示的数据库数据。
  
    这个类的代码相当长,并且大部分代码都只是返回客户端的javas cript代码。下面是类的代码:
  
  <%
  class dhtmlgetrows
  
  '******* private member variables **********
  private irecsperpage
  private strthstring
  '*******************************************
  
  '************ initialize event *************
  private sub class_initialize()
  irecsperpage = 10 'assign a default value
  end sub
  '*******************************************
  
  
  '************ property let/get *************
  public property let thstring(strvalue)
  'replace all apostrophes with \'
  strthstring = replace(strvalue, "'", "\'")
  end property
  
  public property get thstring()
  thstring = strthstring
  end property
  
  
  public property let recsperpage(ivalue)
  if ivalue > 0 and isnumeric(ivalue) then
  irecsperpage = cint(ivalue)
  end if
  end property
  
  public property get recsperpage()
  recsperpage = irecsperpage
  end property
  '*******************************************
  
  
  '**************** methods ******************
  public function generatehtml(objrs)
  
  'begin by getting an array of the data
  dim avalues
  avalues = objrs.getrows()
  
  'find the value of rows and columns
  dim icols, irows
  icols = ubound(avalues, 1)
  irows = ubound(avalues, 2)
  
  dim stroutput
  'display the initial s cript block
  stroutput = "<s cript language=""javas cript"">" & vbcrlf & _
  "var tablerow = new array(" & irows & ");" & vbcrlf & vbcrlf
  
  dim iloop, icolloop, strtmp
  for iloop = 0 to irows
  stroutput = stroutput & "tablerow[" & iloop & "] = '<tr>"
  
  for icolloop = 0 to icols
  'fix apostrophes
  strtmp = replace(avalues(icolloop, iloop),"'", "\'")
  
  'remove carraige returns
  strtmp = replace(strtmp, vbcrlf, "")
  
  stroutput = stroutput & "<td>" & strtmp & "</td>"
  next 'icolloop
  
  stroutput = stroutput & "</tr>';" & vbcrlf
  next 'iloop
  
  'init global varaibles and find out what browser the user is using
  stroutput = stroutput & vbcrlf & vbcrlf & "var first = 0;" & vbcrlf & _
  "var last = " & irecsperpage & ";" & vbcrlf & _
  "var mynav;" & vbcrlf & "if (navigator.appname == ".netscape"")" & _
  vbcrlf & vbtab & "mynav = ""ns"";" & vbcrlf & _
  "if (navigator.appname == ""microsoft inte.net explorer"")" & _
  vbcrlf & vbtab & "mynav = ""ie"";" & vbcrlf & _
  vbcrlf & "</s cript>" & vbcrlf & vbcrlf
  
  'now display the html table
  stroutput = stroutput & vbcrlf & "<div id=""grid""> </div>" & vbcrlf & _
  vbcrlf & vbcrlf & "<s cript language=""javas cript"">" & vbcrlf
  
  
  'write the nav function
  stroutput = stroutput & "function nav(ival) {" & vbcrlf & _
  "// do we want to move forward or backwards?" & vbcrlf & _
  "if (ival == 1) { " & vbcrlf & vbtab & "first += " & _
  irecsperpage & ";" & vbcrlf & "last += " & irecsperpage & _
  vbcrlf & "}" & vbcrlf & "else if (ival == -1) { " & vbcrlf & vbtab & _
  "first -= " & irecsperpage & ";" & vbcrlf & vbtab & "last -= " & _
  irecsperpage & ";" & vbcrlf & "}" & vbcrlf & _
  vbcrlf & vbcrlf & "var txt = '';" & vbcrlf & _
  "txt += '<table border=""1"">';" & vbcrlf
  
  'do we need to add a th string?
  if len(strthstring) > 0 then
  stroutput = stroutput & "txt += '<tr>" & strthstring & "</tr>';" & vbcrlf
  end if
  
  stroutput = stroutput & "for (var iloop = first; iloop < last; iloop++)" & vbcrlf & _
  vbtab & "if (iloop <= " & irows & ") txt += tablerow[iloop];" & vbcrlf & _
  "txt += '</table>';" & vbcrlf & vbcrlf
  
  'now, show next/prev links if applicable
  stroutput = stroutput & "if (first > 0) // show prev link" & vbcrlf & _
  vbtab & "txt += '<a href=""javas cript:nav(-1);"">prev " & _
  irecsperpage & "</a> ';" & vbcrlf & vbcrlf & _
  "if (last <= " & irows & ") // show next link" & vbcrlf & vbtab & _
  "txt += '<a href=""javas cript:nav(1);"">next " & _
  irecsperpage & "</a>';" & vbcrlf & vbcrlf
  
  'write out the new html content to the div tag
  stroutput = stroutput & "// write out the the div tag depending on browser..." & vbcrlf & _
  "if (mynav == ""ns"") {" & vbcrlf & vbtab & _
  "document.layers['grid'].document.write(txt);" & vbcrlf & vbtab & _
  "document.close();" & vbcrlf & "}" & vbcrlf & vbcrlf & _
  "if (mynav == ""ie"")" & vbcrlf & vbtab & _
  "document.all['grid'].innerhtml = txt;" & vbcrlf & vbcrlf & _
  "}" & vbcrlf & vbcrlf
  
  stroutput = stroutput & "nav(0);" & vbcrlf & "</s cript>"
  
  generatehtml = stroutput
  end function
  '*******************************************
  end class
  
  %>
  
  在结束之前,我想要快速地解释一下怎样在一个asp页面中使用这个类。由于这个类只包含一个方法,因此使用这个类相当简单。你所需要做的就是创建并填充一个记录集对象,然后创建一个类的例示,并使用response.write输出objclassinstance.generatehtml(objrs)的值。
  
  
  <!--#include file="dhtmlgetrows.class.asp"-->
  <%
  'create and populate a recordset
  dim objrs, objconn, strsql
  set objconn = server.createobject("adodb.connection")
  objconn.open "dsn=mydsn"
  
  strsql = "select top 25 viewcount, des cription " & _
  "from tblfaq order by viewcount desc"
  
  set objrs = objconn.execute(strsql)
  
  'create an instance of the dhtmlgetrows class
  dim objpagedresults
  set objpagedresults = new dhtmlgetrows
  
  objpagedresults.thstring = "<th>views</th><th>faq question</th>"
  response.write objpagedresults.generatehtml(objrs)
  
  'clean up...
  set objpagedresults = nothing
  
  objrs.close
  set objrs = nothing
  
  objconn.close
  set objconn = nothing
  %>
  
  
     以上的代码片段假定dhtmlgetrows类可以在一个服务器端包含文件dhtmlgetrows.class.asp中使用。
  
  
    


 


关键字 本文所属关键字

相关 与本文相关文章

分类 所有文章关键字导航

源码编程相关

Java   Asp   PHP   .Net   XML   C/C++   CGI   VB   Jsp   J2ee   J2se   J2me   EJB   Servlet   Tomcat   Resin   Struts   Weblogic   Eclipse   ANT   GUI   JMS   Web servise   IDEA   Webphere   Hibernate   Spring   Jboss   Applet   Swing   Socket   Javamail   Perl   Ajax   P2P   安全   模式   框架   测试   开源   游戏

SQL数据库相关

My-SQL   Ms-SQL   Access   DB2   Oracle   Sybase   SQLserver   索引   存储过程   加密   数据库   分页   视图  

手机无线相关

3G   Wap   CDMA   GRPS   GSM   IVR   彩信   短信   无线   增值业务

网页设计制作相关

HTML   CSS   网页配色   网页特效   Javascript   VBscript   Dreamweaver   Frontpage   JS   Web   网站设计

网站建设推广相关

建站经验   网站优化   网站排名   推广   Alexa

操作系统/服务器相关

Windows XP   Windows 2000   Windows 2003   Windows Me   Windows 9.x   Linux   UNIX   注册表   操作系统   服务器   应用服务器

图形图像多媒体相关

Photoshop   Fireworks   Flash   Coreldraw   Illustrator   Freehand   Photoimpact   多媒体   图形图像

标准 网站致力的规范

Valid CSS!

无不良内容,无不良广告,无恶意代码

Valid XHTML 1.0 Transitional

creativecommons