| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 508 人关注过本帖
标题:[求助]DropdownList二级联动代码有问题
只看楼主 加入收藏
重在参与
Rank: 1
等 级:新手上路
帖 子:177
专家分:0
注 册:2006-5-16
收藏
 问题点数:0 回复次数:2 
[求助]DropdownList二级联动代码有问题

两张表:
test test1
uid uname uid tid uname
1 aaa 1 1 第一个1
2 bbb 2 1 第一个2
3 ccc 3 2 第二个
4 ddd 4 3 第三个
我运行a.aspx后,dropdownlist2没有反应,是不是我哪块SQL查询写错了,因为我用别的表好用
a.aspx中HTML的代码:
<script type="text/javascript">
function load(state)
{
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", "b.aspx?state="+state, false);
oHttpReq.send("");
result = oHttpReq.responseText;
oDoc.loadXML(result);
items1 = oDoc.selectNodes("//NewDataSet/Table/tid"); //这个是用DataSet里的数据
items2 = oDoc.selectNodes("//NewDataSet/Table/uname"); //为DropDownList2要显示的字段
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);
   }
}

</script>
<body>
<form id="form1" runat="server">
<div>
<asp:DropDownList id="DropDownList1" runat="server"></asp:DropDownList>
<asp:DropDownList id="DropDownList2" runat="server"></asp:DropDownList>
</div>
</form>
</body>
----------------------------------------------------------------------------------
a.aspx.cs的代码
SqlConnection con = new SqlConnection("server=.;database=pubs;uid=sa");
string sql = "select * from test";
SqlDataAdapter da = new SqlDataAdapter(sql,con);
DataSet ds = new DataSet();
da.Fill(ds);
this.DropDownList1.DataSource = ds.Tables[0].DefaultView;
this.DropDownList1.DataTextField = "uname";
this.DropDownList1.DataValueField = "uid";
this.DropDownList1.DataBind();
// 这里是绑定客户端事件,当第一个DropDownList的选项改变时激发下面的事件onchange,这个事件将调用一个客户端方法load()
this.DropDownList1.Attributes.Add("onchange", "load(this.options[this.selectedIndex].value)");
-------------------------------------------------------------------------------------------------
b.aspx。cs的代码
string shengNo = Request["state"].ToString();
SqlConnection con = new SqlConnection("server=.;database=pubs;uid=sa");
//这是DropDownList2要显示的字段
string sql = "select uid,uname from test1 where tid='" + shengNo + "'";
SqlDataAdapter da = new SqlDataAdapter(sql,con);
DataSet ds = new DataSet();
da.Fill(ds);
con.Close();
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();

搜索更多相关主题的帖子: DropdownList 代码 
2007-07-27 10:13
重在参与
Rank: 1
等 级:新手上路
帖 子:177
专家分:0
注 册:2006-5-16
收藏
得分:0 
不好意思,是我要显示的字段写错了,对不起,打扰各位了,这段代码好用,哪位需要尽管拿去

如果一个人不知道他要驶向哪个码头,那么任何风都不会是顺风。
2007-07-27 10:23
冰彩虹
Rank: 4
来 自:上海
等 级:贵宾
威 望:14
帖 子:806
专家分:44
注 册:2007-6-28
收藏
得分:0 
奇怪,我这怎么可以呀

不同的就是数据库连接符SqlConnection con = new SqlConnection("server=.;database=SMM;uid=sa;pwd=******");

你DropDownList1下拉有数据说明连接符应该没问题,是不是b页面的查询没有满足条件的数据,所以DropDownList2是空的

Flying without wings
2007-07-27 10:42
快速回复:[求助]DropdownList二级联动代码有问题
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.016706 second(s), 9 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved