日期:2011-07-30  浏览次数:21029 次

 我在作asp程序时经常遇到这种情况:
  将数据库中的数据(如所有雇员名)列在列表框中,程序要求进行选择其中一项或多项提交到下一个页面。
  但麻烦的是如果数据(如雇员)过多,会给客户的选择带来麻烦--要从很长的列表中进行数据定位。最好的解决方法是用户在选择前采用模糊查询的方式进行数据定位,准确找出需要选择的雇员名,再选择、提交。
  问题出现在我们程序员面前:如何不刷新页面筛选从数据库中筛选数据。本人的方法有两种:
  1、采用双列表框的方法
  2、采用XML方法
  本文主要介绍第一种方法:采用双列表框的方法
  编程思想:
  采用双列表框,其中一个为隐藏状态,另一个是显示给用户看的列表框。
  程序开始将数据(如雇员姓名)分别装进两个列表框中,当用户需要筛选雇员时首先将显示列表框清空,再更据筛选条件将数据从隐藏列表框装入显示列表框中。
  这样即可实现不刷新页面筛选数据库中的内容。
  实现:
  下面以SQLSERVER为例,筛选NorthWind库中Employees表的雇员名进行说明。

  <html>
  <head>
  <title>不刷新页面查询的方法</title>
  <metahttp-equiv="Content-Type"content="text/html;charset=gb2312">
  </head>
  <scriptlanguage="javascript">
  functionsearch_onclick(){
  file://得到筛选雇员的名字
  searchtext=window.searchContent.value

  file://首先移除在所有查询结果列表中的选项
  j=searchObj.length;
  for(i=j-1;i>=0;i--)
  {
  searchObj.remove(i);
  }
  if(searchtext!=""){
  file://显示符合筛选条件的雇员
  j=searchSource.length;
  for(i=0;i<j;i++){
  searchsource=searchSource.options(i).text;
  k=searchsource.indexOf(searchtext);
  if(k!=-1){
  option1=document.createElement("option");
  option1.text=searchsource;
  option1.value=searchSource.options(i).value;
  searchObj.add(option1);
  }
  }
  }
  else{
  file://如果没有输入查询条件则显示所有雇员
  j=searchSource.length;
  for(i=0;i<j;i++){
  searchsource=searchSource.options(i).text;
  option1=document.createElement("option");
  option1.text=searchsource;
  option1.value=searchSource.options(i).value;
  searchObj.add(option1);
  }
  }
  }


 </script>
  <bodybgcolor="#FFFFFF"text="#000000">
  <%
  servername="wyb"'服务器名
  user="sa"'用户名
  pw=""'用户密码
  databasename="northwind"'数据库名
  setconn=server.CreateObject("adodb.connection")

conn.Open"DRIVER=SQLServer;SERVER="&servername&";UID="&user&";pwd="&pw&";DATABASE="&  databasename
  setrs=server.CreateObject("adodb.recordset")
  sql="Selectemployeeid,lastnamefromemployeesorderbyemployeeid"
  rs.Opensql,conn%>
  <tablewidth="80%"border="1">
  <tr>
  <td>
  <inputtype="text"name="searchContent">
  <inputtype="button"name="Button"value="查  询">
  </td>
  </tr>
  <tr>
  <td>查询结果<br>
  <selectname="searchObj"size="10">
    <%dowhilenotrs.eof%>
  <optionvalue="<%=rs("employeeid")%>"><%=rs("lastname")%></option>
  <%rs.movenext
  loop
    %>
  </select>
  <selectname="searchSource"size="10"style="display:none">
  <%
  rs.movefirst
  dowhilenotrs.eof%>
  <optionvalue="<%=rs("employeeid")%>"><%=rs("lastname")%></option>
  <%rs.movenext
  loop
  %>
  </select>
  </td>
  </tr>
  </table>
  <%rs.close
  setrs=nothing
  %>
  </body>
  </html>