lhd 发表于 2006-10-28 17:25

dropdownlist二級聯動,頁面刷新后一dropdownlist的數據會被清空,為什么呢?

<P>我選擇dropdownlist1中的數據,dropdownlist2會相應顯示另一組數據,但要是頁面刷新后dropdownlist2的數據就會被清空掉,有人知道是為什么嗎??<BR><BR>代碼如下:<BR><BR>webform1.aspx頁面<BR><BR>&lt;head runat="server"&gt;<BR>    &lt;title&gt;Untitled Page&lt;/title&gt;<BR>    <BR>    &lt;script language="javascript"&gt;<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&lt;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>  &lt;/script&gt;</P>
<P><BR>&lt;/head&gt;<BR>&lt;body&gt;<BR>    &lt;form id="form1" runat="server"&gt;<BR>    &lt;div&gt;<BR>    &lt;asp:DropDownList ID="DropDownList1" runat="server" Width="100px"&gt;&lt;/asp:DropDownList&gt;<BR>    &lt;asp:DropDownList ID="DropDownList2" runat="server" Width="100px" onchange="display()"&gt;&lt;/asp:DropDownList&gt;<BR>    &lt;asp:TextBox ID="text1" runat="server" Width="100px"&gt;&lt;/asp:TextBox&gt;<BR>    &lt;asp:TextBox ID="text2" runat="server" Width="100px"&gt;&lt;/asp:TextBox&gt;<BR>    <BR>    &lt;/div&gt;<BR>    &lt;/form&gt;<BR>&lt;/body&gt;<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>

lhd 发表于 2006-10-30 08:47

[em06]咋就會沒人理呢??????

lhd 发表于 2006-10-30 15:05

总在线 7641 人,其中『 XML论坛 』上共有 1 位会员与 6 位客人,今日贴子 1[em03]

页: [1]

编程论坛