【asp源码栏目提醒】:网学会员鉴于大家对asp源码十分关注,论文会员在此为大家搜集整理了“【Dreamweaver ASP_打造站内搜索】 - 项目管理”一文,供大家参考学习
第10章 打造站内搜索 利用站内搜索可以帮助网页浏览者快速检索到站点内任意资源。
用Dreamweaver打造高效的站内搜索时需要考虑到效率、准确性这些方面的因素。
开发过程中应该尽量兼顾到搜索过程及结果的人性化、智能化操作。
10.1 站内地址库的设计 可以把整个站点中的一些重要内容按照一定的规则进行划分。
例如可以依据栏目功能进行归类并且加以注释连同具体URL地址记录到数据库内以备搜索。
下面就具体来介绍整个站内地址数据库Access数据库的设计流程。
1单击【开始】【程序】【Microsoft Office】【Microsoft Office Access 2003】命令打开【Microsoft Access】窗口。
2单击【新建文件】【空数据库】命令弹出【文件新建数据库】对话框。
可以根据实际情况保存到相应位置。
并在【文件名】文本框中输入数据库文件名例如“urllist.mdb”如图10.1所示。
图10.1 【文件新建数据库】对话框 3单击【创建】按钮弹出【urllist:数据库Access 2000 文件格式】窗口如图10.2所示。
·239· 图10.2 【urllist:数据库Access 2000文件格式】窗口 4单击【使用设计器创建表】按钮弹出【表1表】设计窗口。
单击【文件】【保存】命令把表进行重新命名例如“tb_urllist”。
5在【tb_urllist表】设计窗口中按照表10.1所示定义所有字段信息。
表10.1 【tb_urllist表】所有字段信息 字段名称 数据类型 说明 id 自动编号 地址编号 title 文本 地址标题 intro 文本 说明内容 url 文本 url地址 6右击“id”字段选择【主键】命令为表设置主键如图10.3所示。
7单击窗口右上角的【关闭】按钮系统会提示保存所做的修改。
返回到【urllist:数据库Access 2000文件格式】窗口会发现所设计的表“tb_urllist”已经存在于当中如图10.4所示。
图10.3 为表设置主键 图10.4 【urllist:数据库Access 2000文件格式】窗口 8在实际操作中可以双击打开“tb_urllist”表记录管理窗口。
在这个窗口中可以选择录入、编辑或者删除一些记录信息如图10.5所示。
·240· 图10.5 “tb_urllist”表记录管理窗口 9打开Dreamweaver的设计界面。
单击【文件】【新建】命令新建一个标准
ASP文件。
10单击【窗口】【数据库】命令弹出【应用程序】面板如图10.6所示。
11单击【】【自定义连接字符串】命令弹出【自定义连接字符串】对话框。
在【连接名称】文本框中输入“conn”。
在【连接字符串】文本框中输入“DriverMicrosoft Access Driver .mdbDBQi:sitedataurllist.mdb”。
选中【Dreamweaver应连接】选项组下的【使用此计算机的驱动程序】选项如图10.7所示。
图10.6 【应用程序】面板 图10.7 【自定义连接字符串】对话框 12单击【测试】按钮如果连接创建成功将弹出如图10.8所示的对话框。
13单击【确定】按钮关闭【自定义连接字符串】对话框。
返回【应用程序】面板向导将自动在其下拉列表中添加一条【conn】的列表选项如图10.9所示。
14单击【窗口】【文件】命令弹出【文件】面板。
将看到在站点目录下会自动创建一个名为“Connections”的文件夹。
在其下将会新增一个名为“conn.
asp”的文件如图10.10所示。
图10.8 连接创建成功提示 图10.9 【应用程序】面板 图10.10 【文件】面板 ·241·10.2 搜索条件确认 搜索条件确认页面提供表单接口帮助用户输入关键词。
本节讲解如何制作搜索页面。
10.2.1 搜索条件确认页面设计 搜索条件确认页面比较简单由一个文本框和一个【搜索】按钮构成。
当用户输入关键词后单击【搜索】按钮后根据关键词对数据库进行查询。
搜索条件确认页面设计过程如下所示。
1打开Dreamweaver的设计界面。
单击【文件】【新建】命令新建一个名为“add.
asp”的标准
ASP文件。
2单击Dreamweaver【插入】工具栏中【常用】【表格】命令。
在弹出的【表格】对话框中进行相应设置在【文档】窗口中插入了一些表格用于定位。
3根据实际情况可以对特定的单元格进行宽度和背景图片的指定来修饰表格的外观。
在此基础上再对单元格行适当的排版录入相应的文字信息具体效果如图10.11所示。
图10.11 搜索条件录入页面排版 4单击Dreamweaver【插入】工具栏中【表单】【表单】命令在文档最前头插入表单。
通过【标签选择器】选中该表单。
在【属性】检查器中【表单名称】文本框中输入“frmdata”。
在【动作】文本框中输入“search_ok.
asp”。
在【目标】下拉列表中选择【_self】选项如图10.12所示。
图10.12 【属性】检查器 5选中用于输入检索条件的文本框。
在【属性】检查器中【文本域】文本框中输入“keyword”如图10.13所示。
图10.13 文本域属性设置 6通过【标签选择器】选中“frmdata”表单如图10.14所示。
图10.14 【标签选择器】 7单击【窗口】【行为】命令弹出【标签ltformgt】面板如图10.15所示。
8单击【】【检查表单】命令弹出【检查表单】对话框。
在【命名的栏位】列表中分别选择不同的栏位选中其对应的【值】选项组下的【必需的】选项如图10.16所示。
·242· 图10.15 【标签ltformgt】面板 图10.16 【检查表单】对话框 9单击【确定】按钮关闭【检查表单】对话框。
返回【标签ltformgt】面板向导将自动在其下拉列表中添加一条在【onSubmit】事件时发生的行为【检查表单】的列表选项如图10.17所示。
图10.17 【检查表单】行为 10.2.2 生成代码分析——搜索条件确认页面search.
asp 搜索条件确认页面search.
asp完整代码如下 ltLANGUAGEquotVBSCRIPTquot CODEPAGEquot936quotgt ltDOCTYPE html PUBLIC quot-//W3C//DTD XHTML 1.0 Transitional//ENquot quothttp://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtdquotgt lthtml xmlnsquothttp://www.w3.org/1999/xhtmlquotgt ltheadgt ltmeta http-equivquotContent-Typequot contentquottext/html charsetgb2312quot /gt lttitlegt搜索条件确认页面lt/titlegt ltscript typequottext/JavaScriptquotgt lt-- function MM_findObjn d //v4.01 var pix ifd ddocument ifpn.indexOfquotquotgt0ampampparent.frames.length dparent.framesn.substringp1.document nn.substring0p ifxdnampampd.all xd.alln for i0xampampiltd.forms.lengthi xd.formsin fori0xampampd.layersampampiltd.layers.lengthi xMM_findObjnd.layersi.document ifx ampamp d.getElementById xd.getElementByIdn return x function MM_validateForm //v4.0 ·243· var ipqnmtestnumminmaxerrorsargsMM_validateForm.arguments for i0 iltargs.length-2 i3 testargsi2 valMM_findObjargsi if val nmval.name if valval.valuequotquot if test.indexOfisEmail-1 pval.indexOf if plt1 pval.length-1 errors- nm must contain an e-mail address.n else if testR num parseFloatval if isNaNval errors- nm must contain a number.n if test.indexOfinRange -1 ptest.indexOf: mintest.substring8p maxtest.substringp1 if numltmin maxltnum errors- nm must contain a number between min and max.n else if test.charAt0 R errors - nm is required.n if errors alertThe following errors occurred:nerrors document.MM_returnValue errors //--gt lt/scriptgt lt/headgt ltbodygt ltform ACTIONquotsearch_ok.aspquot namequotfrmdataquot targetquot_selfquot idquotfrmdataquot onsubmitquotMM_validateFormtextfieldRreturn document.MM_returnValuequotgt lttable widthquot342quot heightquot40quot borderquot0quot alignquotcenterquot cellpaddingquot0quot cellspacingquot0quot backgroundquotimgs/bg.gifquotgt lttrgt lttd widthquot70quotgtampnbsplt/tdgt lttdgtltinput namequotkeywordquot typequottextquot idquotkeywordquot sizequot25quot /gtlt/tdgt lttd widthquot70quotgtltinput typequotimagequot namequotimageFieldquot srcquotimgs/btn.gifquot /gtlt/tdgt lt/trgt lt/tablegt lt/formgt lt/bodygt lt/htmlgt 10.3 显示搜索结果 10.3.1 显示搜索结果页面设计 1打开Dreamweaver的设计界面。
单击【文件】【新建】命令新建一个名为“list.
asp”的标准
ASP文件。
2单击Dreamweaver【插入】工具栏中【常用】【表格】命令。
在弹出的【表格】对话框中进行相应设置在【文档】窗口中插入了一些表格用于定位。
3根据实际情况可以对特定的单元格进行宽度和背景图片的指定来修饰表格的外观。
在此基础上再对单元格行适当的排版录入相应的文字信息具体效果如图10.18所示。
4单击【窗口】【绑定】命令弹出【应用程序】面板如图10.19所示。
·244· 图10.18 显示搜索结果页面排版 图10.19 【绑定】命令 5单击【】【记录集查询】命令弹出【记录集】对话框。
在【名称】文本框中输入“rs”。
在【连接】下拉列表中选择【conn】选项。
【sql】文本框中输入“SELECT FROM tb_urllist WHERE title like quotamprequest.querystringquotkeywordquotampquot”如图10.20所示。
6单击【确定】按钮关闭【记录集】对话框。
返回【应用程序】面板向导将自动在其下拉列表中添加一条【rs】的列表选项如图10.21所示。
图10.20 【记录集】对话框 图10.21 【应用程序】面板 7在显示“标题”所对应的表格单元格中单击鼠标。
在【应用程序】面板中选中【记录集rs】【title】选项。
单击【绑定】按钮把“title”字段插入到单元格中。
用上述同样的的方法在“说明”、“url地址”所对应的单元格中插入记录集字段如图10.22所示。
8单击【窗口】【服务器行为】命令弹出【应用程序】面板如图10.23所示。
图10.22 插入所有记录集字段 图10.23 【应用程序】面板 ·245·9直接通过鼠标或者借助【标签选择器】选中表格内用于显示数据的所有单元格如图10.24所示。
图10.24 【标签选择器】 10单击【】【重复区域】命令弹出【重复区域】对话框。
在【记录集】下拉列表中选择【rs】选项。
在【显示】选项组文本框中输入“2”如图10.25所示。
11单击【确定】按钮关闭对话框。
12把鼠标定位到用于放置分页信息的区域。
单击【插入】【应用程序对象】【显示记录记数】【记录集导航状态】命令弹出【Recordset Navigation States】对话框。
在【Recordset】下拉列表中选择【rs】选项如图10.26所示。
图10.25 【重复区域】对话框 图10.26 【Recordset Navigation States】对话框 13单击【确定】按钮关闭对话框。
Dreamweave将在文档窗口中自动插入一排用于记录统计的信息如图10.27所示。
图10.27 插入记录集导航状态 14把鼠标定位到用于放置分页控制的按钮区域。
单击【插入】【应用程序对象】【记录集分页】【记录集导航条】命令弹出【记录集导航条】对话框。
在【记录集】下拉列表中选择【rs】选项。
在【显示方式】选项组中选中【文本】选项如图10.28所示。
15单击【确定】按钮关闭对话框。
Dreamweave将在文档窗口中自动插入一排用于翻页控制的导航条如图10.29所示。
图10.28 【记录集导航条】对话框 图10.29 插入记录集导航条 ·246· 10.3.2 测试搜索功能 启动Internet Explorer在地址栏中输入“http://localhost/search.
asp”。
输入检索条件如图10.30所示。
单击【提交】按钮新闻信息会自动录入到数据库。
转到search_ok.
asp页面如图10.31所示。
图10.30 查看“http://localhost/search.
asp” 图10.31 查看http://localhost/search_ok.
asp 10.3.3 生成代码分析——显示搜索结果页面search_ok.
asp 显示搜索结果页面search_ok.
asp完整代码如下 ltLANGUAGEquotVBSCRIPTquot CODEPAGEquot936quotgt lt--include filequotConnections/conn.aspquot --gt lt Dim rs 定义记录集变量 Dim rs_numRows 定义记录当前记录位置的变量 Set rs Server.CreateObjectquotADODB.Recordsetquot 建立记录集对象 rs.ActiveConnection MM_conn_STRING 指定数据库链接字符串 rs.Source quotSELECT FROM tb_urllist WHERE title like quotamprequest.querystringquotkeywordquotampquotquot rs.CursorType 0 rs.CursorLocation 2 rs.LockType 1 rs.Open 获取记录 rs_numRows 0 gt lt Dim Repeat1__numRows 定义显示记录的总数目变量 Dim Repeat1__index 定义显示记录的数目变量 Repeat1__numRows 2 Repeat1__index 0 rs_numRows rs_numRows Repeat1__numRows gt lt Recordset Stats Move To Record and Go To Record: declare stats variables Dim rs_total 定义记录总数变量 Dim rs_first 定义初始行变量 ·247·Dim rs_last 定义结束行变量 set the record count rs_total rs.RecordCount 获取记录集总数 set the number of rows displayed on this page If rs_numRows lt 0 Then rs_numRows rs_total Elseif rs_numRows 0 Then rs_numRows 1 End If set the first and last displayed record rs_first 1 设置开始行为1 rs_last rs_first rs_numRows - 1 计算结束行 if we have the correct record count check the other stats If rs_total ltgt -1 Then 如果记录集总数正确 If rs_first gt rs_total Then rs_first rs_total End If If rs_last gt rs_total Then 如果结束行数大约记录总数 rs_last rs_total End If If rs_numRows gt rs_total Then 如果当前记录行数大于记录总数 rs_numRows rs_total End If End If gt lt Recordset Stats: if we dont know the record count manually count them If rs_total -1 Then 如果获取的记录总数为-1 count the total records by iterating through the recordset rs_total0 设置记录总数为0 While Not rs.EOF 依次循环访问每个记录 rs_total rs_total 1 记录总数加1 rs.MoveNext 访问下一个记录 Wend reset the cursor to the beginning If rs.CursorType gt 0 Then rs.MoveFirst 访问第一个记录 Else rs.Requery 重新查询 End If set the number of rows displayed on this page If rs_numRows lt 0 Or rs_numRows gt rs_total Then rs_numRows rs_total End If set the first and last displayed record rs_first 1 设置开始行为1 rs_last rs_first rs_numRows - 1 If rs_first gt rs_total Then rs_first rs_total ·248· End If If rs_last gt rs_total Then 如果结束行数大约记录总数 rs_last rs_total End If End If gt lt Dim MM_paramName gt lt Move To Record and Go To Record: declare variables Dim MM_rs Dim MM_rsCount Dim MM_size Dim MM_uniqueCol Dim MM_offset Dim MM_atTotal Dim MM_paramIsDefined Dim MM_param Dim MM_index Set MM_rs rs MM_rsCount rs_total MM_size rs_numRows MM_uniqueCol quotquot MM_paramName quotquot MM_offset 0 MM_atTotal false MM_paramIsDefined false If MM_paramName ltgt quotquot Then MM_paramIsDefined Request.QueryStringMM_paramName ltgt quotquot End If gt lt Move To Record: handle index or offset parameter if Not MM_paramIsDefined And MM_rsCount ltgt 0 then use index parameter if defined otherwise use offset parameter MM_param Request.QueryStringquotindexquot If MM_param quotquot Then MM_param Request.QueryStringquotoffsetquot End If If MM_param ltgt quotquot Then MM_offset IntMM_param End If if we have a record count check if we are past the end of the recordset If MM_rsCount ltgt -1 Then If MM_offset gt MM_rsCount Or MM_offset -1 Then past end or move last If MM_rsCount Mod MM_size gt 0 Then last page not a full repeat region MM_offset MM_rsCount - MM_rsCount Mod MM_size Else MM_offset MM_rsCount - MM_size ·249· End If End If End If move the cursor to the selected record MM_index 0 While Not MM_rs.EOF And MM_index lt MM_offset Or MM_offset -1 MM_rs.MoveNext 访问下一个记录 MM_index MM_index 1 Wend If MM_rs.EOF Then MM_offset MM_index set MM_offset to the last possible record End If End If gt lt Move To Record: if we dont know the record count check the display range If MM_rsCount -1 Then walk to the end of the display range for this page MM_index MM_offset While Not MM_rs.EOF And MM_size lt 0 Or MM_index lt MM_offset MM_size MM_rs.MoveNext 访问下一个记录 MM_index MM_index 1 Wend if we walked off the end of the recordset set MM_rsCount and MM_size If MM_rs.EOF Then MM_rsCount MM_index If MM_size lt 0 Or MM_size gt MM_rsCount Then MM_size MM_rsCount End If End If if we walked off the end set the offset based on page size If MM_rs.EOF And Not MM_paramIsDefined Then If MM_offset gt MM_rsCount - MM_size Or MM_offset -1 Then If MM_rsCount Mod MM_size gt 0 Then MM_offset MM_rsCount - MM_rsCount Mod MM_size Else MM_offset MM_rsCount - MM_size End If End If End If reset the cursor to the beginning If MM_rs.CursorType gt 0 Then MM_rs.MoveFirst 访问第一个记录 Else MM_rs.Requery 重新查询 End If move the cursor to the selected record MM_index 0 While Not MM_rs.EOF And MM_index lt MM_offset MM_rs.MoveNext 访问下一个记录 ·250· MM_index MM_index 1 Wend End If gt lt Move To Record: update recordset stats set the first and last displayed record rs_first MM_offset 1 rs_last MM_offset MM_size If MM_rsCount ltgt -1 Then If rs_first gt MM_rsCount Then rs_first MM_rsCount End If If rs_last gt MM_rsCount Then rs_last MM_rsCount End If End If set the boolean used by hide region to check if we are on the last record MM_atTotal MM_rsCount ltgt -1 And MM_offset MM_size gt MM_rsCount gt lt Go To Record and Move To Record: create strings for maintaining URL and Form parameters Dim MM_keepNone Dim MM_keepURL Dim MM_keepForm Dim MM_keepBoth Dim MM_removeList Dim MM_item Dim MM_nextItem create the list of parameters which should not be maintained MM_removeList quotampindexquot If MM_paramName ltgt quotquot Then MM_removeList MM_removeList amp quotampquot amp MM_paramName amp quotquot End If MM_keepURLquotquot MM_keepFormquot.
上一篇:
基于ASP_net的学生成绩管理系统
下一篇:
浅析普外科围手术期抗菌药物的应用