DropDownList联动成功后,第二个DropDownList取不到值为什么?
<script>function load(B_id){
var drp2 = document.getElementById("DropDownList2");
for (i = drp2.length; i >= 0; i--){
drp2.options.remove(i);
}
var oHttpReq = new ActiveXObject("MSXML2.XMLHTTP");
var oDoc = new ActiveXObject("MSXML2.DOMDocument");
oHttpReq.open("POST", "WebForm6.aspx?B_id="+B_id, false);
oHttpReq.send("");
result = oHttpReq.responseText;
oDoc.loadXML(result);
items1 = oDoc.selectNodes("//SClass/Table/Bid");
items2 = oDoc.selectNodes("//SClass/Table/S_name");
var itemsLength=items1.length;
for(i=0;i<itemsLength;i++)
//将小类的类名和编号赋予DropDownList2
{
var newOption = document.createElement("OPTION");
newOption.text=items2[i].text;
newOption.value=items1[i].text;
drp2.options.add(newOption);
}
}
window.onload = function(){load('1');}
</script>
上边是webform1.aspx的js代码,下边是后台代码
string sql;
sql=System.Configuration.ConfigurationSettings.AppSettings["ConnectionString"];
SqlConnection con = new SqlConnection(sql);
SqlDataAdapter da = new SqlDataAdapter("select B_id,Bname from BClass order by B_id desc",con);
DataSet ds = new DataSet();
da.Fill(ds);
DropDownList1.DataSource=ds;
this.DropDownList1.DataTextField = "Bname";
this.DropDownList1.DataValueField = "B_id";
this.DropDownList1.DataBind();
this.DropDownList1.Attributes.Add("onchange","load(this.options[this.selectedIndex].value)");
下边的是WebForm6.aspx里的后台代码,这个页面前台页面什么也没有
if(this.Request["B_id"]!=null)
{
//string B_id = this.Request["B_id"].ToString();
int B_id=int.Parse(Request["B_id"].ToString());
SqlConnection con = new SqlConnection("server=.;database=Product;uid=sa;pwd=hacker;");
SqlDataAdapter da = new SqlDataAdapter("select Bid,S_name from SClass where Bid = "+B_id+"",con);
DataSet ds = new DataSet("SClass");
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();
}
不知道为什么每次在webform1.aspx.cs页取DropDownList2的值的时候总是出错,显示是空的,不知道为什么了麻烦大家费心看看了。
--------------------编程问答-------------------- 调试一下。 --------------------编程问答-------------------- 我先谢谢大家了,很着急的一个问题,因为能力有限,在这摆脱了。 --------------------编程问答-------------------- 数据库能查询出来吗?webform1的代码好像没有错 --------------------编程问答-------------------- 数据是可以查出来的,联动也可以显示,就是取第二个DropDownList的时候没有东西 --------------------编程问答-------------------- 一定要用request.form的形式来获取联动产生的值 --------------------编程问答-------------------- 试试int DropDownList2=int.Parse(Request.Form(this.DropDownList2.SelectedValue.ToString()));是这样吗?不对啊,说为属性,此处为方法不让这么用啊 --------------------编程问答-------------------- up --------------------编程问答-------------------- 接分先
补充:.NET技术 , Web Services