dropdownlist二級聯動,頁面刷新后一dropdownlist的數據會被清空,為什么呢?
<P>我選擇dropdownlist1中的數據,dropdownlist2會相應顯示另一組數據,但要是頁面刷新后dropdownlist2的數據就會被清空掉,有人知道是為什么嗎??<BR><BR>代碼如下:<BR><BR>webform1.aspx頁面<BR><BR><head runat="server"><BR> <title>Untitled Page</title><BR> <BR> <script language="javascript"><BR> <BR> function load()<BR> {<BR> var drp2 = document.getElementById("DropDownList2");<BR> drp2.options.length=0;<BR> var oHttpReq = new ActiveXObject("MSXML2.XMLHTTP"); //发送请求<BR> var oDoc = new ActiveXObject("MSXML2.DOMDocument"); //响应结果<BR> var state=document.getElementById("DropDownList1").value;<BR> oHttpReq.open("POST","webform2.aspx?state="+escape(state), false);<BR> oHttpReq.send("");<BR> result = oHttpReq.responseText; <BR> oDoc.loadXML(result); <BR> items1 = oDoc.selectNodes("//CITY/Table/worker_no");<BR> items2 = oDoc.selectNodes("//CITY/Table/name");<BR> var itemsLength=items1.length;<BR>// alert(itemsLength);<BR> var newOption = document.createElement("OPTION");<BR> newOption.text=""; <BR> newOption.value=""; <BR> drp2.options.add(newOption);<BR> for(i=0;i<itemsLength;i++) <BR> { <BR> var newOption = document.createElement("OPTION"); <BR> newOption.text=items1[i].text; <BR> newOption.value=items2[i].text;<BR> <BR> var newOption2 = document.createElement("OPTION"); <BR> newOption2.text=items2[i].text; <BR> newOption2.value=items2[i].text;<BR> <BR> if(i==itemsLength-1)<BR> {<BR> document.getElementById("text1").value=newOption.text;<BR> document.getElementById("text2").value=newOption2.value;<BR> } <BR> drp2.add(newOption);<BR> }<BR> </P><P> }<BR> <BR> function display()<BR> {<BR> with(document.form1)<BR> {<BR> alert(DropDownList2.options[DropDownList2.selectedIndex].value);<BR> document.forms[0].submit();<BR> }<BR> }<BR> <BR> </script></P>
<P><BR></head><BR><body><BR> <form id="form1" runat="server"><BR> <div><BR> <asp:DropDownList ID="DropDownList1" runat="server" Width="100px"></asp:DropDownList><BR> <asp:DropDownList ID="DropDownList2" runat="server" Width="100px" onchange="display()"></asp:DropDownList><BR> <asp:TextBox ID="text1" runat="server" Width="100px"></asp:TextBox><BR> <asp:TextBox ID="text2" runat="server" Width="100px"></asp:TextBox><BR> <BR> </div><BR> </form><BR></body><BR><BR><BR>webform2.aspx.cs頁面<BR><BR><BR> protected void Page_Load(object sender, EventArgs e)<BR> {<BR> if (Request.QueryString["state"].ToString().Trim() != String.Empty)<BR> {<BR> string state = Request.QueryString["state"].ToString().Trim();<BR> SqlConnection con = DBCon.creatConnection();<BR> con.Open();<BR> SqlDataAdapter da = new SqlDataAdapter("select worker_no,name from employee where department='" + state.Trim()+ "'", con);<BR> DataSet ds = new DataSet("CITY");<BR> da.Fill(ds);<BR> XmlTextWriter writer = new XmlTextWriter(Response.OutputStream, Encoding.UTF8);<BR> writer.Formatting = Formatting.Indented;<BR> writer.Indentation = 4;<BR> writer.IndentChar = ' ';<BR> writer.WriteStartDocument();<BR> ds.WriteXml(writer);<BR> writer.Flush();<BR> Response.End();<BR> writer.Close();<BR> da.Dispose();<BR> con.Close();<BR> }</P>
<P> }<BR><BR><BR><BR>webform1.aspx.cs的頁面代碼就不寫了吧,我在display()中加一句: document.forms[0].submit();<BR><BR>頁面刷新以后dropdownlist2的數據就會被清空掉,不知是什么原因,你們能否告訴我呢??<BR><BR><BR>等待中.....................................<BR></P>
页:
[1]
