请教:
这个二级无联动刷新基本上算完成,有一个不对的地方就是点击完“查看”按钮后,二级下拉框会被清空,请问如何解决这个问题?谢谢回答
(我用的是Oracle数据库,按以下步骤做的话可直接执行。如果是Sql Server或Mysql的话,请对DB部分和DataSource以及.cs文件中相关部分进行修改即可。)
【DB】(Sql*Plus里执行)
- SQL code
-------------------------------------------- parent------------------------------------------drop table kout_phone;create table kout_phone( id int, phone_value int, phone_text varchar2(50))/insert into kout_phone values (1, 100, 'MOTO');insert into kout_phone values (2, 101, 'Nokia');commit;select * from kout_phone;/* Result: ID PHONE_VALUE PHONE_TEXT---------- ----------- -------------------------------------------------- 1 100 MOTO 2 101 Nokia*/-------------------------------------------- child------------------------------------------create table kout_phone_type( id int, parent_id int, type_value int, type_text varchar2(50))/insert into kout_phone_type values (1, 100, 10001, 'MOTOAAA');insert into kout_phone_type values (2, 100, 10002, 'MOTOBBB');insert into kout_phone_type values (3, 100, 10003, 'MOTOCCC');insert into kout_phone_type values (4, 101, 10101, 'NOKIAAAA');insert into kout_phone_type values (5, 101, 10102, 'NOKIABBB');insert into kout_phone_type values (6, 101, 10103, 'NOKIACCC');insert into kout_phone_type values (7, 101, 10104, 'NOKIADDD');insert into kout_phone_type values (8, 101, 10105, 'NOKIAEEE');commit;select * from kout_phone_type;/* Result: ID PARENT_ID TYPE_VALUE TYPE_TEXT---------- ---------- ---------- -------------------------------------------------- 1 100 10001 MOTOAAA 2 100 10002 MOTOBBB 3 100 10003 MOTOCCC 4 101 10101 NOKIAAAA 5 101 10102 NOKIABBB 6 101 10103 NOKIACCC 7 101 10104 NOKIADDD 8 101 10105 NOKIAEEE*/
【DDL2JiLianDong.aspx】
- HTML code
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="DDL2JiLianDong.aspx.cs" Inherits="DDL2JiLianDong" %><!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"><html xmlns="http://www.w3.org/1999/xhtml" ><head runat="server"> <title>DDL 2 Ji Lian Dong</title> <script language="javascript"> //jb函数会根据不同的浏览器初始化个xmlhttp对象 function jb() { var A=null; try { A=new ActiveXObject("Msxml2.XMLHTTP"); } catch(e) { try { A=new ActiveXObject("Microsoft.XMLHTTP"); } catch(oc) { A=null } } if ( !A && typeof XMLHttpRequest != "undefined" ) { A=new XMLHttpRequest() } return A } //下面Go函数是父列表框改变的时候调用,参数是选择的条目 function Go(obj) { //得到选择框的下拉列表的value var svalue = obj.value; //定义要处理数据的页面 var weburl = "DDL2JiLianDong.aspx?parent_id="+svalue; //初始化个xmlhttp对象 var xmlhttp = jb(); //提交数据,第一个参数最好为get,第三个参数最好为true xmlhttp.open("get",weburl,true); // alert(xmlhttp.responseText); //如果已经成功的返回了数据 xmlhttp.onreadystatechange=function() { if(xmlhttp.readyState==4)//4代表成功返回数据 { var result = xmlhttp.responseText;//得到服务器返回的数据 //先清空dListChild的所有下拉项 document.getElementById("dListChild").length = 0; //给dListChild加个全部型号的,注意是Option不是option document.getElementById("dListChild").options.add(new Option("全部型号","0")); if(result!="")//如果返回的数据不是空 //alert(result); { //把收到的字符串按照,分割成数组 var allArray = result.split(","); //循环这个数组,注意是从1开始,因为收到的字符串第一个字符是,号,所以分割后第一个数组为空 for(var i=1;i<allArray.length;i++) { //在把这个字符串按照|分割成数组 var thisArray = allArray[i].split("|"); //为dListChild添加条目 document.getElementById("dListChild").options.add(new Option(thisArray[1].toString(),thisArray[0].toString())); } } } } //发送数据,请注意顺序和参数,参数一定为null或者"" xmlhttp.send(null); } </script></head><body> <form id="Form1" method="post" runat="server"> <%-- DataSource for Parent --%> <asp:SqlDataSource ID="sdsParent" runat="server" ConnectionString="<%$ ConnectionStrings:ConnectionString %>" ProviderName="<%$ ConnectionStrings:ConnectionString.ProviderName %>" SelectCommand="select phone_value, phone_text from kout_phone unionselect 0, '全部型号' from kout_phone order by phone_value asc"> </asp:SqlDataSource> <asp:DropDownList ID="dListParent" runat="server" DataSourceID="sdsParent" DataValueField="phone_value" DataTextField="phone_text" onchange="Go(this)"></asp:DropDownList> <asp:DropDownList id="dListChild" runat="server" Width="120px"> <asp:ListItem Value="0">全部型号</asp:ListItem> </asp:DropDownList> <asp:Button id="Button1" runat="server" Text="查看" OnClick="Button1_Click"></asp:Button> <br /> <br /> ParentValue: <asp:Label ID="lblParentValue" runat="server" ></asp:Label> <br /> <br /> ChildValue: <asp:Label ID="lblChildValue" runat="server" ></asp:Label> <!-- for dListChild --> <input id="myHidden" type="hidden" runat="server" value="0"/></form> </body></html>