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