|
导读现在,我们可以更进一步,把类别表显示出来,让用户可以通过选择不同的类别来查看不同的内容。该怎样解决呢?我们可以这样来考虑:1、把类别的选择也看作一种查询2、为了节省页面空间,把类别放在下拉列表框中,... 现在,我们可以更进一步,把类别表显示出来,让用户可以通过选择不同的类别来查看不同的内容。该怎样解决呢?我们可以这样来考虑: 1、把类别的选择也看作一种查询 2、为了节省页面空间,把类别放在下拉列表框中,同时提供“所有类别”项,让用户可以浏览全部信息 因此,我们把类别放在 query.inc 中 另外,我们把 theScript 和 myconnstr 、thePageSize 都放入 function.inc 中,这样页面结构看起来更加合理。 注意 getScriptName() 函数,它从文件名取得 ASP 的名字,文件名后面的 add、delete、edit、view 作为系统的关键字,被过滤,例如: theActionScript = getScriptName("sample_edit.asp") theActionScript 值为 "sample" 这样我们可以对某个表的增删改操作用不同的文件来实现,例如: list_add.asp添加记录 list_delete.asp删除 list_edite.asp修改 list_view.asp查看 在以后的篇章中,我们就会用到 theActionScript,您会发现有了它,很多功能都可以总结、合并。 请看 sample3.asp <一> 需要分页的 ASP 文件 sample3.asp <!--#include file="../inc/functions.inc"--> <% '////////////////////////////////////////////////////////// ' ' 定义表名 ' '////////////////////////////////////////////////////////// theTableName= "addressbook" '////////////////////////////////////////////////////////// ' ' 查询条件 ' '////////////////////////////////////////////////////////// theQueryField= "fld" & theTableName & "_nickname"' 查询字段,完整名字 theQueryTitle= "昵称"' 字段显示标题 theQueryTable= "vw" & theTableName' 字段所在的表,完整名字 theQueryClass= theTableName & "_class"' 类别表名,去掉 tbl、vw 前缀 theClassId= c2int(request("classid"))' 当前类别号 ' 如果是查询模式,则构造模糊查询语句 if request("mode") = "query" then %><!--#include file="../inc/control/query_result.inc"--><% else ' 否则忽略 theQueryCon = "1>0" end if '////////////////////////////////////////////////////////// ' ' 限制条件 ' '////////////////////////////////////////////////////////// theLimitCon= "fld" & theTableName & "_userid=" & Session("userid") if theClassId > 0 then theLimitCon = theLimitCon & " and fld" & theQueryClass & "id=" & theClassId end if '////////////////////////////////////////////////////////// ' ' 构造 SQL 语句 ' '////////////////////////////////////////////////////////// uSQL = "select * from " & theQueryTable & " where ( " & theQueryCon & " ) and ( " & theLimitCon & " ) " %> <!--#include file="../inc/control/navigator_init.inc"--> <html> <head> <title>test</title> <meta http-equiv="Content-Type" content="text/html; charset=gb2312"> <link rel="stylesheet" href="../default.css" type="text/css"> </head> <!-- 你的 HTML 代码//--> <table width="100%" border="0" cellspacing="0" cellpadding="0"> <tr> <td> <!--查询控制--> <!--#include file="../inc/control/query.inc"--> </td> <td> <!--导航控制--> <!--#include file="../inc/control/navigator.inc"--> </td> </tr> <!-- 你的 HTML 代码//--> <table width="100%" border="0" cellspacing="0" cellpadding="0"> <tr> <td> <!--显示本页记录--> <% For i = 1 To rs.pagesize %> <!-- 你的记录显示代码//--> <% rs.MoveNext If rs.EOF Then Exit For rs.close conn.close End If next %> </td> </tr> <!-- 你的 HTML 代码//--> </body> </html> <二> 查询控制代码: query.inc 注意 form 表单一定要有自己的名字,例如 form_query ,以和其他表单区分 <table width="100%" border="0" cellspacing="0" cellpadding="0" height="25"> <form method="post" action="<%=theScript%>?mode=query" name=form_query onsubmit="javascript:return checkquery(this);"> <tr> <% if theQueryClass = "" then uTextSize = "" %> <td> <% else '显示类别列表 uTextSize = "size=""8""" %> <td width=120> 类别 <select class=form_select name="classid" onchange="javascript:this.form.submit();"> <% ListClass theQueryClass, theClassId, 1 %> </select> <% end if %> </td> <td> <%=theQueryTitle%> <input type="text" name="<%=theQueryField%>" value="<%=request(theQueryField)%>" class="form_text" <%=uTextSize%>> <input type="submit" name="form_query_submit" value="查询" class="form_button"> </td> </tr> </form> <script language="javascript"> function checkquery(form) { if(form.<%=theQueryField%>.value=='') { alert('请输入<%=theQueryTitle%>信息。\n提示:输入 * 可以查询所有内容。'); form.<%=theQueryField%>.focus(); return false; } return true; } </script> <三> 构造模糊查询语句 query_result.inc <% '////////////////////////////////////////////////////////// ' ' 构造模糊查询语句 ' '////////////////////////////////////////////////////////// ' 模糊查询只支持字符串 theQueryValue = trim(request(theQueryField)) if theQueryValue = "*" or theQueryValue = "" then theQueryCon = "1>0" else theQueryCon = theQueryField & " like '%" & theQueryValue & "%' " end if ' 如果有分类表,则需要加上分类限制 if theQueryClass <> "" then if theClassId > 0 then theQueryCon = theQueryCon & " and fldclassid=" & theClassId end if end if %> <四> 函数集 function.inc <% '-------------------------------------------------------- ' ' '初始化变量 ' ' '-------------------------------------------------------- ' 取得当前 ASP 页面的完整路径,重要技巧 theScript= Request.ServerVariables("SCRIPT_NAME") '从文件名取得 ASP 的名字 theActionScript= getScriptName(theScript) myconnstr= "driver={SQL Server};server=yourserver;uid=sa;pwd=;database=yourdatabase" thePageSize= 20' 每页显示的记录数 theLength_Listclass= 8' 类别列表中类别宽度 '-------------------------------------------------------- 'Name:C2int 'Argument: 'Return: 'Description:转换为 Int 型 '-------------------------------------------------------- Function C2int(byval uStr) ' 11, boolean if vartype(uStr) = 11 then if uStr = false then C2int = 0 else C2int = 1 end if else uStr = trim(uStr) if uSTr = "" then C2int = 0 else C2int = Cint(uStr) end if end if End Function '-------------------------------------------------------- 'Name:ListClass 'Argument:uTableClass类别表名字 'uSelClassId当前选择的 Classid 'uAllClass是否显示“所有类别”:1 显示 / 0 显示一个空的选项 'Return: 'Description:显示类别表 '-------------------------------------------------------- Sub listClass(uTableClass,uSelClassId,uAllClass) dim uListLength, uStr, uConn, uRs, uClassId, uClass_sel, uClassTitle, uSQL uClass_sel= "" if uAllClass = 1 then if uSelClassId = 0 then uClass_sel = "selected" end if uStr = uStr & "<option value=0 " & uClass_sel & ">所有类别</option>" else uStr = uStr & "<option>" & string(theLength_Listclass/2 - 1," ") & "</option>" end if set uConn= Server.Createobject("adodb.connection") uConn.open myconnstr set uRs = server.createobject("adodb.recordset") uSQL = "select * from tbl" & uTableClass & " order by fld" & uTableClass & "_title" uRs.open uSQL,uConn,1,3 do while not uRs.eof uClass_sel= "" uClassId= uRs("fld" & uTableClass & "_id") uClassTitle= leftX(uRs("fld" & uTableClass & "_title"),theLength_listClass) if uClassId = uSelClassId then uClass_sel = "selected" end if uStr = uStr & "<option value=""" & uClassId & """ " & uClass_sel & ">" & uClassTitle & "</option>" uRs.movenext loop response.write uStr rsClose uRs cnClose uconn End Sub '-------------------------------------------------------- 'Name:getScriptName 'Argument: 'Return: 'Description: 从文件名取得 ASP 的名字 '文件名后面的 add、delete、edit、view 作为系统的关键字,被过滤,例如: 'getScriptName("list_add.asp") 返回 "list" 。 ' '这样我们可以对某个表的增删改操作用不同的文件来实现,例如: ' 'list_add.asp添加记录 'list_delete.asp删除 'list_edite.asp修改 'list_view.asp查看 '-------------------------------------------------------- Function getScriptName(byval str) dim uStr0,uStr1,uStr2,uMax,uTestStr,i,uKeyWords uKeyWords = "add,delete,edit,view" uKeyWords = split(uKeyWords,",") uStr0 = Split(str, ".") uStr1 = Split(uStr0(0), "/") uStr2 = Split(uStr1(ubound(uStr1)), "_") uMax = ubound(uStr2) uTestStr = uStr2(uMax) getScriptName = uStr2(0) for each element in uKeyWords if uTestStr = element then uMax = uMax - 1 exit for end if next For i = 1 To uMax getScriptName = getScriptName + "_" + uStr2(i) Next End Function %> |
温馨提示:喜欢本站的话,请收藏一下本站!