是根据ms提供的代码修改而成,目前还不支持编辑,可以排序、查询、分页显示
<%@ language=vbscript %>
<% option explicit %>
<%
class classdatagrid
private m_strsql
private m_strconn
private m_strrowcolor1
private m_strrowcolor2
private m_strmode
private m_strprocesspage
private m_strtitle
private m_strrsname
private m_strfindfields
public property get sql()
sql = m_strsql
end property
public property let sql(strsql)
m_strsql = strsql
end property
public property get conn()
conn = m_strconn
end property
public property let conn(strconn)
m_strconn = strconn
end property
public property get rowcolor1()
if isnull(m_strrowcolor1) or len(m_strrowcolor1) = 0 then
rowcolor1 = "#ffffff"
else
rowcolor1 = m_strrowcolor1
end if
end property
public property let rowcolor1(strrowcolor1)
m_strrowcolor1 = strrowcolor1
end property
public property get rowcolor2()
if isnull(m_strrowcolor2) or len(m_strrowcolor2) = 0 then
rowcolor2 = "#00ffff"
else
rowcolor2 = m_strrowcolor2
end if
end property
public property let rowcolor2(strrowcolor2)
m_strrowcolor2 = strrowcolor2
end property
public property get mode()
if isnull(m_strmode) or len(m_strmode) = 0 then
mode = "view"
else
mode = m_strmode
end if
end property
public property let mode(strmode)
if strmode <> "view" and strmode <> "edit" then
response.write ("模式错误——只允许使用view和edit<br>")
response.end
else
m_strmode = strmode
end if
end property
public property get processpage()
processpage = m_strprocesspage
end property
public property let processpage(strprocesspage)
m_strprocesspage = strprocesspage
end property
public property get title()
if isnull(m_strtitle) or len(m_strtitle) = 0 then
title = "data grid"
else
title = m_strtitle
end if
end property
public property let title(strtitle)
m_strtitle = strtitle
end property
public property get rsname()
if isnull(m_strrsname) or len(m_strrsname) = 0 then
rsname = "grid"
else
rsname = m_strrsname
end if
end property
public property let rsname(strrsname)
m_strrsname = strrsname
end property
public property get findfields()
findfields = m_strfindfields
end property
public property let findfields(strfindfields)
m_strfindfields = strfindfields
end property
sub showdatagrid()
dim intpagenum
dim objconn
dim objrs
dim intabs
dim intcurrentpage
dim intfindcol
dim intpagesize
dim introw
dim intcol
dim i
dim intpos
dim intdisplayrows
dim strsort
dim strsortdir
dim strlastsort
dim strlastsortdir
dim strcolor
dim strfind
dim boolfind
dim boolfound
dim strfindfields
dim strcurrentpage
const aduseclient = 3
const adopendynamic = 2
const adasyncfetchnonblocking = &h40
const adsearchforward = 1
const adchar = 129
const advarchar = 200
if isarray(findfields) then
strfindfields = findfields
end if
if not isobject(session(rsname)) and (isnull(sql) or len(sql) = 0) then
response.write ("你必须设置sql属性以得到结果集<br>")
response.end
end if
if not isobject(session(rsname)) and (isnull(conn) or len(conn) = 0) then
response.write ("你必须设置sql属性以连接数据库<br>")
response.end
end if
if mode = "edit" and (isnull(processpage) or len(processpage) = 0) then
response.write ("你必须设置processpage属性以运行edit模式<br>")
response.end
end if
strcurrentpage = request.servervariables("path_info")
if instr(1, strcurrentpage, "/") > 0 then strcurrentpage = right(strcurrentpage, len(strcurrentpage) – instrrev(strcurrentpage, "/"))
if isobject(session(rsname)) and request.querystring("reload") <> "y" then
set objrs = session(rsname)
else
set objconn = server.createobject("adodb.connection")
set objrs = server.createobject("adodb.recordset")
set session(rsname) = objrs
objconn.open conn
objrs.cursorlocation = aduseclient
objrs.source = sql
objrs.cursortype = adopendynamic
objrs.properties("initial fetch size") = 11
set objrs.activeconnection = objconn
objrs.open , , , , adasyncfetchnonblocking
set objrs.activeconnection = nothing
objconn.close
end if
if trim(request("sortby")) <> "" and trim(request("resort")) <> "" then
strsort = request("sortby")
intpos = instr(2, objrs.sort, "]")
if intpos > 0 then
strlastsort = left(objrs.sort, intpos)
strlastsortdir = trim(mid(objrs.sort, intpos + 2))
end if
if trim(strsort) <> trim(strlastsort) then
strsortdir = "asc"
else
if strlastsortdir = "asc" then
strsortdir = "desc"
else
strsortdir = "asc"
end if
end if
objrs.sort = strsort & " " & strsortdir
end if
intpagesize = 10
if trim(request("txtpagesize")) <> "" then
intpagesize = request("txtpagesize")
end if
intpagenum = trim(request.querystring("pagenum"))
if (trim(request("lstpages")) <> "" or intpagenum <> "") and trim(request("allrecs")) = "" then
if intpagenum <> "" then
intcurrentpage = intpagenum
else
intcurrentpage = request("lstpages")
end if
else
intcurrentpage = 1
end if
if not (objrs.bof and objrs.eof) then
objrs.pagesize = intpagesize
if cint(intcurrentpage) > cint(objrs.pagecount) then
intcurrentpage = objrs.pagecount
end if
end if
session("pagenum") = intcurrentpage
boolfind = false
if trim(request("findcol")) <> "" and _
trim(request("findit")) <> "" and _
trim(request("find" & request("findcol"))) <> "" and _
(objrs.recordcount > objrs.pagesize) then
boolfind = true
intfindcol = cint(request("findcol"))
strfind = "[" & objrs(intfindcol).name & "] like %" & _
request("find" & intfindcol) & "%"
intabs = objrs.absoluteposition
objrs.filter = strfind
boolfound = false
if objrs.absoluteposition < 1 then
objrs.absoluteposition = intabs
else
boolfound = true
intcurrentpage = int(objrs.absoluteposition / objrs.pagesize)
if objrs.absoluteposition mod objrs.pagesize <> 0 then
intcurrentpage = intcurrentpage + 1
end if
intabs = objrs.absoluteposition
session("pagenum") = intcurrentpage
end if
else
objrs.filter = ""
if not (objrs.bof and objrs.eof) then objrs.absolutepage = intcurrentpage
end if
%>
<script language="javascript">
<% if boolfind and not boolfound then %>
window.status=** 字符串未找到 **
<% end if %>
function refresh()
{
document.frmreport.submit();
}
function movetopage(pagenumber)
{
if (pagenumber != -1)
{document.frmreport.lstpages[pagenumber].selected = true;}
else
{document.frmreport.lstpages[0].selected = true;}
refresh();
}
function showallrecs()
{
document.frmreport.txtpagesize.value = <%=objrs.recordcount%>;
document.frmreport.allrecs.value = "yes"
refresh();
}
function resort(sortstring)
{
document.frmreport.sortby.value = sortstring;
document.frmreport.resort.value = "yes";
refresh();
}
function dofind(colnum)
{
document.frmreport.findcol.value = colnum;
document.frmreport.findit.value = "yes";
refresh();
}
</script>
<center>
<hr>
<table border="0" width="100%">
<tr>
<td align="left">
<b><%=now()%></b>
</td>
<td align="center">
<%=title%>
</td>
<td align="right">
<%if not (objrs.bof and objrs.eof) then%>
<b><%= objrs.recordcount%> 条纪录
(共 <%=objrs.pagecount%> 页 第 <%=intcurrentpage%> 页 )</b>
<%end if%>
</td>
</tr>
</table>
<hr>
<p><font style="color:red; font-weight:bold"><%=session("msg")%></font></p>
<%session("msg") = ""%>
</center>
<form name="frmreport" method="post" action="<%=strcurrentpage%>">
<table cellspacing="2" cellpadding="2" border="0" width="100%">
<tr>
<td align="center" nowrap>
<a href="javascript:refresh()" title="应用新的设置" onmouseover="window.status=刷新; return true" onmouseout="window.status=; return true">
刷新</a>
</td>
<td nowrap>
</td>
<td align="center" nowrap>
<a href="javascript:showallrecs()" title="在一屏显示所有纪录" onmouseover="window.status=显示所有纪录; return true" onmouseout="window.status=; return true">
显示所有纪录</a>
</td>
<td align="right" valign="top" width="100%" nowrap>
<%if not (objrs.bof and objrs.eof) then%>
<b>每页
<input type="text" size="3" name="txtpagesize" value="<%=intpagesize%>">
条纪录 </b>
<%if objrs.pagecount > 1 then%>
<b>转到第
<select size="1" name="lstpages" onchange="refresh();">
<%for introw=1 to objrs.pagecount%>
<%if cint(intcurrentpage) = cint(introw) then%>
<option selected value="<%=introw%>"><%=introw%>
<%else%>
<option value="<%=introw%>"><%=introw%>
<%end if%>
<%next%>
</select>页</b>
<%end if%>
<%else%>
<input type="hidden" name="txtpagesize" value="<%=intpagesize%>">
<%end if%>
</td>
</tr>
</table>
<%if not (objrs.bof and objrs.eof) then%>
<table border="1" cellpadding="0" width="100%">
<tr>
<td>
<table border="0" cellpadding="2" cellspacing="0" width="100%">
<tr>
<%for intcol = 0 to objrs.fields.count – 1%>
<th nowrap valign="top" align="left">
<b>
<%
if isarray(findfields) then
boolfound = false
for i = 0 to ubound(strfindfields)
if ucase(objrs(intcol).name) = ucase(strfindfields(i)) then
%>
<input type="button" value="查询" onclick="javascript:dofind(<%=intcol%>)" onmouseover="window.status=在<%=objrs(intcol).name%>中查询指定字符串" onmouseout="window.status=">
<input type="text" name="find<%=intcol%>" size="5" maxlength="5" value="<%if boolfind then response.write(request("find" & intcol))%>"><br>
<%
boolfound = true
exit for
end if
next
if not boolfound then
response.write("<br>")
end if
end if
%>
<a href="javascript:resort([<%=objrs(intcol).name%>])" onmouseover="window.status=按照<%=objrs(intcol).name%>排序" onmouseout="window.status=;" title="按照<%=objrs(intcol).name%>排序">
<%=objrs(intcol).name%></a></b>
</th>
<%next%>
</tr>
<%intdisplayrows = objrs.absoluteposition + objrs.pagesize – 1%>
<%for introw = objrs.absoluteposition to intdisplayrows%>
<tr>
<%if cbool( instr(1, cstr(introw / 2), ".") > 0) then
strcolor = rowcolor1
else
strcolor = rowcolor2
end if%>
<%for intcol = 0 to objrs.fields.count – 1%>
<td nowrap style="background:<%=strcolor%>">
<%=objrs.fields(intcol).value%></td>
<%next%>
</tr>
<%objrs.movenext%>
<%if objrs.eof then exit for%>
<%next%>
<%
if objrs.recordcount > objrs.pagesize then
if boolfind then
objrs.absoluteposition = intabs
else
if objrs.eof then
objrs.absoluteposition = objrs.recordcount – objrs.pagesize
else
objrs.absoluteposition = objrs.absoluteposition – objrs.pagesize
end if
end if
end if
%>
</table>
</td>
</tr>
</table>
<table border="0" cellspacing="2" cellpadding="2" align="left">
<tr>
<%
if (intcurrentpage > 1) then%>
<td align="center" width="55">
<a href="javascript:movetopage(document.frmreport.lstpages.selectedindex – 1)" onmouseover="window.status=上一页;" onmouseout="window.status=;" title="上一页">
上一页</a>
</td>
<%end if%>
<%
if cint(intcurrentpage) < cint(objrs.pagecount) then%>
<td align="center" width="55">
<a href="javascript:movetopage(document.frmreport.lstpages.selectedindex + 1)" onmouseover="window.status=下一页;" onmouseout="window.status=;" title="下一页">
下一页</a>
</td>
<%end if%>
</tr>
</table>
<%else%>
<center>
<table border="0" cellpadding="2">
<tr>
<td align="center">
没找到匹配的纪录。
</td>
</tr>
</table>
</center>
<%end if%>
<input type="hidden" name="sortby" value="<%response.write(strsort)%>">
<input type="hidden" name="resort">
<input type="hidden" name="findcol" value="<%response.write(intfindcol)%>">
<input type="hidden" name="findit">
<input type="hidden" name="allrecs">
</form>
<%
set objrs = nothing
end sub
end class
%>
<%
dim mydatagrid
set mydatagrid = new classdatagrid
mydatagrid.sql = "yoursql"
mydatagrid.conn = "yourconnstr"
mydatagrid.rowcolor1 = "silver"
mydatagrid.rowcolor2 = "gray"
mydatagrid.mode = "view"
mydatagrid.title = "title"
mydatagrid.rsname = "grid"
mydatagrid.findfields = array("允许查询的字段1","允许查询的字段2")
mydatagrid.showdatagrid
%>
补充一点
class classdatagrid到end class一段建议放inc里面去
还有,require script engine 5.0 or higher
