网络编程
位置:首页>> 网络编程>> Asp编程>> 下拉列表两级连动的新方法(一)

下拉列表两级连动的新方法(一)

 来源:asp之家 发布时间:2009-06-04 18:18:00 

标签:asp,下拉,列表,select

这是网站制作很流行的下拉列表两级连动的代码,也是很多人使用的代码。

下面,先来看这段代码:

<%dim count
set rs=server.createobject("adodb.recordset")
rs.open "select * from shop_Nclass order by Nclassidorder ",conn,1,1%>
<script language = "JavaScript">
var onecount;
onecount=0;
subcat = new Array();
<%
   count = 0
   do while not rs.eof 
%>
subcat[<%=count%>] = new Array("<%= trim(rs("Nclass"))%>","<%= rs("anclassid")%>","<%= rs("Nclassid")%>");
<%
        count = count + 1
        rs.movenext
        loop
        rs.close
%>
  
onecount=<%=count%>;
function changelocation(locationid)
    {
    document.myform.Nclassid.length = 0; 
    var locationid=locationid;
    var i;
    for (i=0;i < onecount; i++)
        {
            if (subcat[i][1] == locationid)
            { 
             document.myform.Nclassid.options[document.myform.Nclassid.length] = new Option(subcat[i][0], subcat[i][2]);
            }        
        }
        
    }    
</script>

在<body>..</body>之间:

<%
        rs.open "select * from shop_anclass order by anclassidorder",conn,1,1
if rs.eof and rs.bof then
response.write "请先添加栏目。"
response.end
else
%>

大类 
     

  <select name="anclassid" size="1" id="anclassid" onChange="changelocation(document.myform.anclassid.options[document.myform.anclassid.selectedIndex].value)" class="wenbenkuang">
                <option selected value="<%=rs("anclassid")%>"><%=trim(rs("anclass"))%></option>
                <%      dim selclass
         selclass=rs("anclassid")
        rs.movenext
        do while not rs.eof
%>
                <option value="<%=rs("anclassid")%>"><%=trim(rs("anclass"))%></option>
                <%
        rs.movenext
        loop
end if
        rs.close
%>
              </select>

小类 
              

<select name="Nclassid" class="wenbenkuang">
                <%rs.open "select * from shop_Nclass where anclassid="&selclass ,conn,1,1
if not(rs.eof and rs.bof) then
%>
                <option selected value="<%=rs("NclassID")%>"><%=rs("Nclass")%></option>
                <% rs.movenext
do while not rs.eof%>
                <option value="<%=rs("NclassID")%>"><%=rs("Nclass")%></option>
                <% rs.movenext
loop
end if
        rs.close
        set rs = nothing
%>
              </select>

从整体上看,这个代码没有什么问题,我也曾经使用过这个代码,认为很时用。但是随着时间的迁移,它的问题也一点点的暴露出来,我们一起来想一下:当供选择的项越来越多时或访问此页面的人越来越多时,数据库的压力也越来越大,最后终于让服务器不堪重负,网页打开越来越慢,最后重新,然后再访问,再变慢,再重启。难道就没有什么有效的方法吗?

 答案:当然是有的。

我们都知道,VBScript是在服务器端运行的,而JScript是在客户端运行的,如果把VBScript的代码使用JScript的代码代替,那么就减少了服务器端的压力,关键是如何转化成JScript代码呢?

接着阅读续篇

0
投稿

猜你喜欢

手机版 网络编程 asp之家 www.aspxhome.com