有的时候做管理界面的添加删除修改重复劳动很麻烦
试写了一个函数包含了分页显示添加删除修改 dim arrHeaderName,arrFieldName,arrFieldData,arrTdWidth,strTblName,strKeyName,strHeaderCss,strBodyCss,strTableCss,strButtomCss,iPageSize,iTableBorder,iMpdifyMethod arrHeaderName=array("编号","起始地址","结束地址","国家","地点") arrFieldName=array("id","onip","offip","addj","addf") arrFieldData=array("auto","num","num","char","char") arrTdWidth=array("50","100","100","150","250") strTblName="ip" strKeyName="id" strHeaderCss="HeaderCss" strBodyCss="BodyCss" strTableCss="TableCss" strButtomCss="ButtomCss" iPageSize=20 iTableBorder=1 iModifyMethod=7 '数据表格(标题名数组,字段名数组,字段类型数组[auto:自动编号,num:数字型,char:字符型(备注型),date,日期型,time:时间型],单元格宽度数组,表名,标题样式,正文样式,表格整体样式,底部样式,分页数,表格边框,修改需求[0:无1:添加2:删除3:修改4:添加+修改5:删除+修改6:添加+删除7:添加+删除+修改]) DataGrid arrHeaderName,arrFieldName,arrFieldData,arrTdWidth,strTblName,strKeyName,strHeaderCss,strBodyCss,strTableCss,strButtomCss,iPageSize,iTableBorder,iModifyMethod
这个函数就完成了具有分页显示添加删除修改一个表中的几个字段功能的页面
http://www.musecn.com/new
函数如下:
<%@LANGUAGE="VBSCRIPT" CODEPAGE="936"%> <% '定义全局变量 dim objConn '信息过滤(信息,类型) function MyRequest(info,iType) if iType=0 then MyRequest=trim(cstr(Replace(request(info),"'","''"))) else if isnumeric(request(info)) then MyRequest=clng(request(info)) else Response.write "类型错误" Response.End end if end if end function '页面头部(页面标题,样式地址) sub PageStart(strPageTitle,strPageCss) response.write "<html>"&vbcrlf response.write "<head>"&vbcrlf response.write "<meta http-equiv=""Content-Type"" content=""text/html; charset=gb2312"">"&vbcrlf response.write "<link href="""&strPageCss&""" rel=""stylesheet"" type=""text/css"">"&vbcrlf response.write "<title>"&strPageTitle&"</title>"&vbcrlf response.write "</head>"&vbcrlf response.write "<body>"&vbcrlf end sub '连接数据库(数据库名) sub DbConn(DbName) set objConn=server.CreateObject("adodb.connection") objConn.open "driver={microsoft access driver (*.mdb)};dbq="&server.MapPath(DbName) end sub sub PageLast() response.write "</body>"&vbcrlf response.write "</html>"&vbcrlf end sub '数据表格(标题名数组,字段名数组,字段类型数组[auto:自动编号,num:数字型,char:字符型(备注型),date,日期型,time:时间型],单元格宽度数组,表名,主键名,标题样式,正文样式,表格整体样式,底部样式,分页数,表格边框,修改需求[0:无1:添加2:删除3:修改4:添加+修改5:删除+修改6:添加+删除7:添加+删除+修改]) sub DataGrid(arrHeaderName,arrFieldName,arrFieldData,arrTdWidth,strTblName,strKeyName,strHeaderCss,strBodyCss,strTableCss,strButtomCss,iPageSize,iTableBorder,iModifyMethod) dim objRs dim strExec dim iTmp,iTmp2 dim iPageCount dim iPage dim iRecordCount dim iPageStart dim iPageEnd dim iLastTenPage dim iNextTenPage set objRs=server.CreateObject("adodb.recordset") objRs.open "select count(*) from "&strTblName,objConn,1,1 iRecordCount=objRs(0) objRs.close If iRecordCount mod iPageSize=0 Then iPageCount= iRecordCount\iPageSize Else iPageCount= iRecordCount\iPageSize + 1 End If iPage=MyRequest("iPage",1) if iPage<1 then iPage=1 if iPage>iPageCount then iPage=iPageCount if MyRequest("Method",0)="Delete" then strExec="delete from "&strTblName&" where "&strKeyName&"="&MyRequest(strKeyName,1) objConn.execute strExec response.redirect "?iPage="&iPage end if if MyRequest("Method",0)="ModifyPost" then strExec="update "&strTblName&" set " for iTmp=0 to ubound(arrHeaderName) if arrFieldName(iTmp)<>strKeyName then if arrFieldData(iTmp)="num" then strExec=strExec&arrFieldName(iTmp)&"="&MyRequest(arrFieldName(iTmp),0) else strExec=strExec&arrFieldName(iTmp)&"='"&MyRequest(arrFieldName(iTmp),0)&"'" end if if iTmp<>ubound(arrFieldName) then strExec=strExec&"," end if next strExec=strExec&" where "&strKeyName&"="&MyRequest(strKeyName,1) objConn.execute strExec response.redirect "?iPage="&iPage end if if MyRequest("Method",0)="AddNew" then strExec="insert into "&strTblName&"(" for iTmp=0 to ubound(arrFieldName) if arrFieldName(iTmp)<>strKeyName then strExec=strExec&arrFieldName(iTmp) if iTmp<>ubound(arrFieldName) then strExec=strExec&"," end if next strExec=strExec&")values(" for iTmp=0 to ubound(arrFieldName) if arrFieldName(iTmp)<>strKeyName then if arrFieldData(iTmp)="num" then strExec=strExec&MyRequest(arrFieldName(iTmp),0) else strExec=strExec&"'"&MyRequest(arrFieldName(iTmp),0)&"'" end if if iTmp<>ubound(arrFieldName) then strExec=strExec&"," end if next strExec=strExec&")" objConn.execute strExec response.redirect "?iPage="&iPage end if
if iPage-5>0 then iPageStart=iPage-5 else iPageStart=1 end if if 10+iPageStart<=iPageCount then iPageEnd=10+iPageStart else iPageEnd=iPageCount end if if 10+iPage<=iPageCount then iNextTenPage=iPage+10 else iNextTenPage=iPageCount end if if iPage-10>0 then iLastTenPage=iPage-10 else iLastTenPage=1 end if
strExec="select top "&iPageSize*iPage&" " strExec=strExec&strKeyName&"," for iTmp=0 to ubound(arrFieldName) strExec=strExec&arrFieldName(iTmp) if iTmp<>ubound(arrFieldName) then strExec=strExec&"," next strExec=strExec&" from "&strTblName objRs.open strExec,objConn,1,1 objRs.move iPageSize*(iPage-1) response.write "<table border=""0"" cellpadding=""0"" cellspacing="""&iTableBorder&""" class="""&strTableCss&""">"&vbcrlf response.write "<tr class="""&strHeaderCss&""">"&vbcrlf for iTmp=0 to ubound(arrHeaderName) response.write "<td width="""&arrTdWidth(iTmp)&""">"&arrHeaderName(iTmp)&"</td>"&vbcrlf next if iModifyMethod=2 or iModifyMethod=5 or iModifyMethod=6 or iModifyMethod=7 then response.write "<td width=""50"">删除</td>" end if if iModifyMethod=3 or iModifyMethod=4 or iModifyMethod=5 or iModifyMethod=7 then response.write "<td width=""50"">修改</td>" end if response.write "</tr>"&vbcrlf if iModifyMethod=1 or iModifyMethod=4 or iModifyMethod=6 or iModifyMethod=7 then response.write "<form name=""addnew"" method=""get""><tr class="""&strBodyCss&""">"&vbcrlf for iTmp=0 to ubound(arrHeaderName) if arrFieldName(iTmp)<>strKeyName then response.write "<td><input name="""&arrFieldName(iTmp)&""" type=""text"" size="""&arrTdWidth(iTmp)/10&"""></td>"&vbcrlf else response.write "<td>-</td>"&vbcrlf end if next response.write "<td colspan=""2""><input type=""hidden"" name=""iPage"" value="""&iPageCount&"""><input type=""hidden"" name=""Method"" value=""AddNew""><input type=""button"" value=""新增记录"" onclick=""addnew.submit()""></td>" response.write "</tr></form>"&vbcrlf end if for iTmp2=1 to iPageSize if objRs.Eof then Exit For response.write "<form name=""modify"&iTmp2&""" method=""get""><tr class="""&strBodyCss&""">"&vbcrlf for iTmp=0 to ubound(arrHeaderName) if arrFieldName(iTmp)<>strKeyName and MyRequest("Method",0)="Modify" and objRs(strKeyName)=MyRequest(strKeyName,1) then response.write "<td><input name="""&arrFieldName(iTmp)&""" type=""text"" size="""&arrTdWidth(iTmp)/10&""" value="""&objRs(arrFieldName(iTmp))&"""></td>"&vbcrlf else response.write "<td>"&objRs(arrFieldName(iTmp))&"</td>"&vbcrlf end if next if iModifyMethod=2 or iModifyMethod=5 or iModifyMethod=6 or iModifyMethod=7 then response.write "<td width=""50""><input type=""button"" onclick=""location.href='?iPage="&iPage&"&Method=Delete&"&strKeyName&"="&objRs(strKeyName)&"'"" value=""删除""></td>" end if if iModifyMethod=3 or iModifyMethod=3 or iModifyMethod=5 or iModifyMethod=7 then if MyRequest("Method",0)="Modify" and objRs(strKeyName)=MyRequest(strKeyName,1) then response.write "<td width=""50""><input type=""hidden"" name=""Method"" value=""ModifyPost""><input type=""hidden"" name="""&strKeyName&""" value="""&objRs(strKeyName)&"""><input type=""hidden"" name=""iPage"" value="""&iPage&"""><input type=""submit"" value=""确认""></td>" else response.write "<td width=""50""><input type=""button"" onclick=""location.href='?iPage="&iPage&"&Method=Modify&"&strKeyName&"="&objRs(strKeyName)&"'"" value=""修改""></td>" end if end if response.write "</tr></form>"&vbcrlf objRs.movenext next response.write "<tr class="""&strButtomCss&""">"&vbcrlf response.write "<td colspan="""&ubound(arrHeaderName)+3&""">"&vbcrlf response.write "分页 ( "&iPage&"/"&iPageCount&" ) " response.write "<a href=""?iPage=1"" title=首页><font face=webdings >9</font></a> " response.write "<a href=""?iPage="&iLastTenPage&""" title=上十页><font face=webdings>7</font></a> " for iTmp=iPageStart to iPageEnd response.write " <a href=""?iPage="&iTmp&""">"&iTmp&"</a>" next response.write " <a href=""?iPage="&iNextTenPage&""" title=下十页><font face=webdings>8</font></a>" response.write " <a href=""?iPage="&iPageCount&""" title=末页><font face=webdings>:</font></a> " response.write " " response.write "</td>"&vbcrlf response.write "</tr>"&vbcrlf response.write "</table>"&vbcrlf end sub %>
调用函数的页面
<!--#include file="func.asp"--> <% starttime=timer() '定义函数参数 dim strPageTitle,strPageCss strPageTitle="标题" strPageCss="css.css" '页面头部(页面标题,样式地址) PageStart strPageTitle,strPageCss '定义函数参数 dim DbName DbName="ip.mdb" '连接数据库(数据库名) DbConn DbName '定义函数参数 dim arrHeaderName,arrFieldName,arrFieldData,arrTdWidth,strTblName,strKeyName,strHeaderCss,strBodyCss,strTableCss,strButtomCss,iPageSize,iTableBorder,iMpdifyMethod arrHeaderName=array("编号","起始地址","结束地址","国家","地点") arrFieldName=array("id","onip","offip","addj","addf") arrFieldData=array("auto","num","num","char","char") arrTdWidth=array("50","100","100","150","250") strTblName="ip" strKeyName="id" strHeaderCss="HeaderCss" strBodyCss="BodyCss" strTableCss="TableCss" strButtomCss="ButtomCss" iPageSize=20 iTableBorder=1 iModifyMethod=7 '数据表格(标题名数组,字段名数组,字段类型数组[auto:自动编号,num:数字型,char:字符型(备注型),date,日期型,time:时间型],单元格宽度数组,表名,标题样式,正文样式,表格整体样式,底部样式,分页数,表格边框,修改需求[0:无1:添加2:删除3:修改4:添加+修改5:删除+修改6:添加+删除7:添加+删除+修改]) DataGrid arrHeaderName,arrFieldName,arrFieldData,arrTdWidth,strTblName,strKeyName,strHeaderCss,strBodyCss,strTableCss,strButtomCss,iPageSize,iTableBorder,iModifyMethod '页面底部() PageLast %> 页面执行时间<%=(timer()-starttime)*1000%>毫秒

|