当前位置:编程学习 > asp >>

我在开发过程总结的一套实现常用功能的函数

答案:<%
Option Explicit
'===================================================
public function MyNowNumber()
MyNowNumber=year(now)& month(NOw) & day(NOw) & hour(NOw) & Minute(Now) & Second(Now)
end function

public function sDataGrid(SqlStr,ConnStr,PageSize,PageNum,beginField,EndField,IDField,HttpStr,PageInfo)'以表格形式显示数据

'这个函数本打算用来实现点击表头排序功能,可是后来总是随机性无故出错,所以就没在现用了,哪们高手可以看看


'DataGrid功能:
' 将数据以表格形式显出来,
' 根据需要可能确定显示的字段,
' 页号,每页显示的记录数
' 复选框绑定的字段
' 修改数据时所连接到的设定的网页,传递的参数名是Idfield

'调用实例 call DataGrid("SELECT * FROM mater_bcode","Driver={SQL Server};uid=sa;pwd=passed;database=cthpdb;server=scb-web",10,20,2,50,0,"http://www.clkhome/mater_code1/tools/aaa.asp","Null")


'参数说明
'sqlstr: 将要查询的sql语句
'connstr: 数据库连接字符串
'PageSize: 数据集每页的记录数,PageSize="MAX"时不分页
'PageNum: 数据集中将要显示的页号
'beginField:在记录集中开始显示的字段位置
'EndField: 在记录集中结束显示的字段位置
'IDField: 用于给复选框的value赋值的字段在记录集中的位置,可以用于提交到其它页,其它页做处理的依据
' 如果IDField<0 or IDField> rs.fields.count或不是数字 则不显示复选框
'HttpStr 修改记录时连接到的网页,传递的是IDField的值,如果 httpstr="0"则不显示修改连接
'PageInfo 确定是否显示"第1页,共1页"的提示,PageInfo="Null"时不显示
'返回值, 返回的是记录集的当前页号


on error resume next
dim conn,rs
'if isnumeric(pageSize)then rs.pageSize=pageSize
set conn=server.CreateObject ("adodb.connection")
set rs=server.CreateObject ("adodb.recordset")
Conn.open Connstr
rs.Open sqlstr,conn,1,3
if err.number<>0 then
Response.Write writeinfo("<BR>DataGrid函数在运行出现了错误!<BR>错描述:" & err.Description & "<BR>")
DataGrid="Err"
exit function
end if
if rs.RecordCount<1 then
Response.Write "<font size='-1' color='#FF0000'>&nbsp;没有数据...</font>"
DataGrid=0
exit function
end if

'------------对参数据进行处理----------------------
'----------确定如何显示字段-----------------
if not Isnumeric(beginField) or beginField<0 then
beginField=0'确保开始显示的位置在合理范围内
else
if beginField>rs.Fields.Count-1 then beginField=rs.Fields.Count-1'保证到少显示一个字段
if beginField<0 then beginField=0
end if
if not Isnumeric(EndField) then EndField=rs.Fields.Count-1
if EndField>rs.Fields.Count-1 then EndField=rs.Fields.Count-1
if EndField<=beginField or EndField<0 then EndField=beginField'保证到少显示一个字段
'----------复选框、修改链接的处理在成生表格时同步完成--------------

'---------分页处理------------------
if Ucase(Trim(PageSize))="MAX" then
PageSize=rs.RecordCount
rs.pageSize=rs.RecordCount
PageNum=1
else
if not Isnumeric(PageSize) or PageSize<1 then PageSize=10
if PageSize>rs.recordcount then pageSize=rs.recordcount
rs.pageSize=pageSize
'---------页号处理----------------
if Trim(Ucase(pageNum))="MAX" then PageNum=rs.PageCount
if not Isnumeric(PageNum) then PageNum=1
if PageNum<1 then PageNum=1
if cint(PageNum-rs.PageCount)>0 then PageNum=rs.PageCount
end if
Rs.AbsolutePage=PageNum

if err.number<>0 then
dbinfo="数据库连接错误"
DataGrid=0
exit function
else
if rs.RecordCount<1 then
Response.Write "没有找到记录"
DataGrid=0
exit function
else
if Ucase(trim(PageInfo))<>"NULL" then '确定是显示页号信息
Response.Write "<font size='-1' color='#666666'>第<font color='#FF0000'>" & PageNum & "</font>页,共<font color='#FF0000'>" & rs.PageCount & "</font>页"
end if
'写入表头
Response.Write "<table width='100%' border='0' cellspacing='1' cellpadding='0' bgcolor='#999999'>"
Response.Write "<tr bgcolor='#CCCCCC'>"
Response.Write "<td width='1%' align='center'><b><font color='#666666' size='-1'>序</font></b></td>"
dim i
for i=beginField to EndField
Response.Write "<td align='center'><b><font color='#666666' size='-1'>" & rs.Fields(i).Name & "</font></b></td>"
next
if Isnumeric(IDfield) then '复选框及修改链接处理
if IDField>=0 and (IDField - rs.fields.count<=0) then
Response.Write "<td width='1%' align='center'><b><font color='#666666' size='-1'>删</font></b></td>"
if trim(httpstr)<>"0" then Response.Write "<td width='1%'><b><font color='#666666' size='-1'>修</font></b></td>"
end if
end if
Response.Write "</tr>"
'写入字段信息
dim RecordNum,FieldNum
for RecordNum=0 to PageSize-1 '?????????????????
Response.Write "<tr bgcolor='#FFFFFF'>"
Response.Write "<td><font size='-1'>" & RecordNum +1+(PageNum-1)*PageSize & "</font></td>"
for FieldNum=beginField to EndField '写入字段值
'response.write "<input type='text' name='textfield' value='" & Trim(rs.Fields(FieldNum).Value) & "'>"
'if Trim(rs.Fields(FieldNum).Value)="" or Isnull(rs.Fields(FieldNum).Value) then
' Response.Write "<td>&nbsp;</td>"
' else
Response.Write "<td><font size='-1'><input type='text' name='textfield' style=' border-top-width: 0px; border-right-width: 0px; border-bottom-width: 0px; border-left-width: 0px' value='" & Trim(rs.Fields(FieldNum).Value) & "'></font></td>"
'end if
next
if Isnumeric(IDfield) then '复选框处理
if IDField>=0 or (IDField - rs.fields.count<=0) then
Response.Write "<td><input type='checkbox' name='IDfield' value='"& rs.Fields(IDfield).Value &"'></td>"
if trim(httpstr)<>"0" then Response.Write "<td><font size='-1'><a href=> end if
end if
Response.Write "</tr>"
rs.MoveNext
if rs.eof then exit for '最后不到一整页时,也跳出
next
Response.Write "</table>"
end if
end if
rs.Close
conn.Close
set rs=nothing
set conn=nothing
Err.Clear
DataGrid=PageNum
end function






public function DataGrid(SqlStr,ConnStr,PageSize,PageNum,beginField,EndField,IDField,HttpStr,PageInfo)'以表格形式显示数据

'DataGrid功能:
' 将数据以表格形式显出来,
' 根据需要可能确定显示的字段,
' 页号,每页显示的记录数
' 复选框绑定的字段
' 修改数据时所连接到的设定的网页,传递的参数名是Idfield

'调用实例 call DataGrid("SELECT * FROM mater_bcode&quo

上一个:无限级目录树最优算法的新研究
下一个:dreamweaverMX通用分页代码研究

CopyRight © 2012 站长网 编程知识问答 www.zzzyk.com All Rights Reserved
部份技术文章来自网络,