DropDownList做三级联动时,一二级可以联动,二三级可联动,但是一二三一起就不行了

Layne1 2016-03-28 08:50:50
新手一个~最近在做级联遇到点问题!
调试运行的时候打开页面如下:

这时候1,2,3 三个下拉列表数据正确,逻辑是对的。但是当我改变1的内容后,2会变并且正确,但是3就不变了,如下图

这时候我再改变2的时候,3会变并且正确。。。唯独只改变1的时候 3不变。。。



后台代码:
protected void Page_Load(object sender, EventArgs e)
{
//判断是否第一次进入页面,如果是,则绑定数据库;如果不是,则无需绑定。
if (!this.IsPostBack)
{
//绑定河流

SqlConnection con = new SqlConnection("Server=.;Uid=sa;Pwd=123456;Database=szpg_new;MultipleActiveResultSets=true");
con.Open();
string cmdText = "select* from River";
SqlCommand cmd = new SqlCommand(cmdText, con);
SqlDataReader sdr = cmd.ExecuteReader();
this.DropDownList1.DataSource = sdr;
this.DropDownList1.DataTextField = "RiverName";//文本内容
this.DropDownList1.DataValueField = "RiverCode"; //数据源字段
this.DropDownList1.DataBind();
//sdr.Close();

//绑定区段
string cmdZoneText = "select* from Zone where RiverCode=" + this.DropDownList1.SelectedValue;
SqlCommand cmdZone = new SqlCommand(cmdZoneText, con);
sdr = cmdZone.ExecuteReader();

this.DropDownList2.DataSource = sdr;
this.DropDownList2.DataTextField = "ZoneName";//文本内容
this.DropDownList2.DataValueField = "ZoneCode"; //数据源字段
this.DropDownList2.DataBind();
//绑定断面

string cmdSectionText = "select* from Section1 where ZoneCode=" + this.DropDownList2.SelectedValue;
SqlCommand cmdSection = new SqlCommand(cmdSectionText, con);

sdr = cmdSection.ExecuteReader();

this.DropDownList3.DataSource = sdr;
this.DropDownList3.DataTextField = "SectionName";//文本内容
this.DropDownList3.DataValueField = "SectionCode"; //数据源字段
this.DropDownList3.DataBind();
//关闭连接

con.Close();
}
}
protected void DropDownList1_SelectedIndexChanged(object sender, EventArgs e)
{
//河流的ID
string RiverCode = this.DropDownList1.SelectedValue;
SqlConnection con = new SqlConnection("Server=.;Uid=sa;Pwd=123456;Database=szpg_new");
con.Open();

SqlCommand cmd = new SqlCommand("select * from Zone where RiverCode=" + RiverCode, con);
SqlDataReader sdr = cmd.ExecuteReader();
//绑定
this.DropDownList2.DataSource = sdr;
this.DropDownList2.DataTextField = "ZoneName";
this.DropDownList2.DataValueField = "ZoneCode";
this.DropDownList2.DataBind();
//sdr.Close();
//con.Close();
}
protected void DropDownList2_SelectedIndexChanged(object sender, EventArgs e)
{
string ZoneCode = this.DropDownList2.SelectedValue;
SqlConnection con = new SqlConnection("Server=.;Uid=sa;Pwd=123456;Database=szpg_new");
con.Open();

SqlCommand cmd = new SqlCommand("select * from Section1 where ZoneCode=" + ZoneCode, con);
SqlDataReader sdr = cmd.ExecuteReader();
//绑定
this.DropDownList3.DataSource = sdr;
this.DropDownList3.DataTextField = "SectionName";
this.DropDownList3.DataValueField = "SectionCode";
this.DropDownList3.DataBind();
}


...全文
284 9 打赏 收藏 转发到动态 举报
写回复
用AI写文章
9 条回复
切换为时间正序
请发表友善的回复…
发表回复
lutaotony 2016-03-30
  • 打赏
  • 举报
回复
没有关联3的所以3不变,都被上面的回答结束了
正怒月神 版主 2016-03-29
  • 打赏
  • 举报
回复
这种一般都是逻辑问题。
人生导师 2016-03-29
  • 打赏
  • 举报
回复
just59277 2016-03-29
  • 打赏
  • 举报
回复
你在1 和2 的选项改变的的事件里面打上断点调试一下。另外 你的SqlConnection 这样写比较好: Using(SqlConnection con = new SqlConnection("Server=.;Uid=sa;Pwd=123456;Database=szpg_new")) { }
Justin-Liu 2016-03-29
  • 打赏
  • 举报
回复
1触发2变的时候没有触发3变
myhope88 2016-03-29
  • 打赏
  • 举报
回复
代码逻辑没写好,你变1的时候,2与3都要清空下
楠小南 2016-03-29
  • 打赏
  • 举报
回复
做个联动 还要服务端亲力亲为。。
不错。有前途。。
Layne1 2016-03-29
  • 打赏
  • 举报
回复
昨晚后来又仔细检查了一下 确实是逻辑问题 1的事件里面只写了2变 没写3变 把1里面的事件写全就好了

62,270

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术交流专区
javascript云原生 企业社区
社区管理员
  • ASP.NET
  • .Net开发者社区
  • R小R
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

.NET 社区是一个围绕开源 .NET 的开放、热情、创新、包容的技术社区。社区致力于为广大 .NET 爱好者提供一个良好的知识共享、协同互助的 .NET 技术交流环境。我们尊重不同意见,支持健康理性的辩论和互动,反对歧视和攻击。

希望和大家一起共同营造一个活跃、友好的社区氛围。

试试用AI创作助手写篇文章吧