对asp.net实现DropDownList、ListBox无刷新三级联动(javascript)的改进

上篇文章说明了如何实现三级联动,但如果数据量较大,加载时会慢一些,因此经过测试,将需要读取的数据先写入一个文本文件中,然后在加载页面时直接调用此文本文件的内容,省去了读取数据库浪费的时间,数据量大时速度比先前快了许多。
数据库请见上一篇文章“asp.net实现DropDownList、ListBox无刷新三级联动(javascript)”

生成数据文本文件的代码
         // 将地区信息写入文本文件area.js
        
// 添加二级地区
         string  strAreaData, strSQL;
        strAreaData 
=   " <script language = 'JavaScript' type='text/javascript'>var onecount;onecount=0;subcat = new Array(); " ;
        strSQL 
=   " select * from nts_area where oneid<>0 and twoid<>0 and threeid=0 order by sort " ;
        IDataReader dr 
=  DataClass.GetDataReader(strSQL);
        
int  i1  =   0 ;
        
while  (dr.Read())
        
{
            strAreaData 
+= "subcat[" + i1.ToString() + "] = new Array('" + dr["areaname"].ToString() + "','" + dr["oneid"].ToString() + "','" + dr["twoid"].ToString() + "');";
            i1
++;

        }

        strAreaData 
+=   " onecount= "   +  i1.ToString()  +   " ; " ;

        
// 添加三级地区
        strAreaData  +=   " var onecount1;onecount1=0;subcat1 = new Array(); " ;

        strSQL 
=   " select * from nts_area where oneid<>0 and twoid<>0 and threeid<>0 order by sort " ;
        dr 
=  DataClass.GetDataReader(strSQL);
        
int  n1  =   0 ;
        
while  (dr.Read())
        
{
            strAreaData 
+= "subcat1[" + n1.ToString() + "] = new Array('" + dr["areaname"].ToString() + "','" + dr["oneid"].ToString() + "','" + dr["twoid"].ToString() + "','" + dr["threeid"].ToString() + "');"; n1++;

        }

        dr.Close();
        strAreaData 
+=   " onecount1= "   +  n1.ToString()  +   " ;</script> " ;

        StreamWriter str;
        File.Delete(Server.MapPath(
" ~/js/area.js " ));

        str 
=  File.CreateText(Server.MapPath( " ~/js/area.js " ));
        str.WriteLine(strAreaData);
        str.Close();

aspx页面代码
<%-- 地区无刷新显示    --%>              
<%  Response.WriteFile(Server.MapPath( " ~/js/area.js " ));  %>
    
< script language  =   " JavaScript "  type = " text/javascript " >

    function changearea1()
    
{
    document.all(
"<% =Ddl_AreaTwo.ClientID %>").length = 0
    document.all(
"<% =Ddl_AreaTwo.ClientID %>").options[0= new Option('选择城市','0');
    document.all(
"<% =Ddl_AreaThree.ClientID %>").length = 0
    document.all(
"<% =Ddl_AreaThree.ClientID %>").options[0= new Option('选择城市','0');
    var oneid
=document.all("<% =Ddl_AreaOne.ClientID %>").value;
    var i;
    
for (i=0;i < onecount; i++)
        
{
            
if (subcat[i][1== oneid)
            

                document.all(
"<% =Ddl_AreaTwo.ClientID %>").options[document.all("<% =Ddl_AreaTwo.ClientID %>").length] = new Option(subcat[i][0], subcat[i][2]);
            }
        
        }

        
    }

    
        function changearea2()
    
{
    document.all(
"<% =Ddl_AreaThree.ClientID %>").length = 0
    document.all(
"<% =Ddl_AreaThree.ClientID %>").options[0= new Option('选择城市','0');
    var twoid
=document.all("<% =Ddl_AreaTwo.ClientID %>").value;
    var oneid
=document.all("<% =Ddl_AreaOne.ClientID %>").value;
    var i;
    
for (i=0;i < onecount1; i++)
        
{
            
if (subcat1[i][2== twoid)
            

                
if (subcat1[i][1== oneid)
                
{
                    document.all(
"<% =Ddl_AreaThree.ClientID %>").options[document.all("<% =Ddl_AreaThree.ClientID %>").length] = new Option(subcat1[i][0], subcat1[i][3]);
                }
        
            }

        }

    }
      
    
    
</ script >                 
                    
< asp:DropDownList ID = " Ddl_AreaOne "  runat = " server "   AppendDataBoundItems = " True " >
                        
< asp:ListItem Value = " 0 " > 请选择 </ asp:ListItem >
                    
</ asp:DropDownList >
                    
< asp:DropDownList ID = " Ddl_AreaTwo "  runat = " server "   >
                        
< asp:ListItem Value = " 0 " > 请选择 </ asp:ListItem >
                    
</ asp:DropDownList >
                    
< asp:DropDownList ID = " Ddl_AreaThree "  runat = " server "   >
                        
< asp:ListItem Value = " 0 " > 请选择 </ asp:ListItem >
                    
</ asp:DropDownList >

cs页面代码
     protected   void  Page_Load( object  sender, EventArgs e)
    
{
        Ddl_AreaOne.Attributes.Add(
"onchange""changearea1()");
        Ddl_AreaTwo.Attributes.Add(
"onchange""changearea2()");
        
string strSQL;
        strSQL 
= "select * from nts_area where oneid<>0 and twoid=0 and threeid=0 order by sort";
        Ddl_AreaOne.DataSource 
= DataClass.GetDataReader(strSQL);    //此为我写的读取DataReader对象的类
        Ddl_AreaOne.DataTextField 
= "areaname";
        Ddl_AreaOne.DataValueField 
= "oneid";
        Ddl_AreaOne.DataBind();
    }

你可能感兴趣的:(JavaScript)