介绍
这篇文章将为大家详细讲解有关ASP.NET中怎么利用DropDownList和列表框实现两级联动功能,文章内容质量较高,因此小编分享给大家做个参考,希望大家阅读完这篇文章后对相关知识有一定的了解。
<强>一、设置前台界面,在Web窗体中添加DropDownList和列表框两个控件。强>
界面图如下所示。
,,,
<强>二、编写后台代码
强>在这,后台代码编写在其窗体的Page_Load事件中
& lt; span 祝辞,protected void employee (object ,发送方,EventArgs e), {大敌; if 才能;(! Page.IsPostBack ),//判断页面是否第一次加载, ,{大敌; SqlConnection 才能;con =, DB.createConnection();,//此方法在上一篇文章中已经介绍,调用一个已经编写好的创建数据库连接的方法只 SqlCommand 才能;cmd =, new SqlCommand (“select  *,得到province",反对),, SqlDataReader 才能;sdr =, cmd.ExecuteReader (),, this.DropDownList1.DataTextField =,才能“proName",, this.DropDownList1.DataValueField =,才能“proID",,//主键字段, this.DropDownList1.DataSource 才能=,特别提款权,, this.DropDownList1.DataBind才能(),, sdr.Close才能(),, , ,,}, , ,}& lt;/span>
编写DropDownList1_SelectedIndexChanged事件代码,实现单击“省”,列表框自动添加该“省”所具有的“市”
& lt; span 祝辞,protected void DropDownList1_SelectedIndexChanged (object ,发送方,EventArgs e), {大敌; this.ListBox1.Items.Clear才能(),, SqlConnection 才能;con2 =, DB.createConnection (),, SqlCommand 才能;cmd1 =, new SqlCommand (“select  *,得到city where proID=? +, this.DropDownList1.SelectedValue,, con2),, SqlDataReader 才能;sdr1 =, cmd1.ExecuteReader (),, while 才能;(sdr1.Read ()), ,{大敌; this.ListBox1.Items.Add才能(new 列(sdr1.GetString (2), sdr1.GetInt32 (0) .ToString ())),, ,,}, ,}& lt;/span>
运行文件,效果图如下所示
关于ASP.NET中怎么利用DropDownList和列表框实现两级联动功能就分享到这里了,希望以上内容可以对大家有一定的帮助,可以学到更多知识。如果觉得文章不错,可以把它分享出去让更多的人看的到。