《ASP.NET》数据绑定—DropDownList、ListBox
2017-07-03 09:27
465 查看
DropDownList和ListBox实现两级联动功能。他们也能够将从后台数据库中搜选的出来的信息加以绑定。这里要实现的功能是在DropDownList中选择“省”,然后让ListBox自己主动将其省份下的“市”显示出来,这就是所谓的两级联动功能,这个功能我们在非常多注冊网页上看见。今天咱们就用ASP.NET解开其神奇的面纱。
一、设置前台界面,在Web窗口中加入DropDownList和ListBox两个控件。界面图例如以下所看到的。
二、编写后台代码
在这,后台代码编写在其窗口的Page_Load事件中
编写DropDownList1_SelectedIndexChanged事件代码,实现单击“省”,ListBox自己主动加入该“省”所具有的“市”
这里河北省的城市我没有加入完整,仅仅是为了实现两级联动的功能,相比前两篇博客中Web控件GridView和Repeater的使用,GridView和Repeater功能尽管是相当强大,可是不同的控件有不同的用途。在这里,杀鸡焉用牛刀?
一、设置前台界面,在Web窗口中加入DropDownList和ListBox两个控件。界面图例如以下所看到的。
二、编写后台代码
在这,后台代码编写在其窗口的Page_Load事件中
<span style="font-family:KaiTi_GB2312;font-size:18px;"> protected void Page_Load(object sender, EventArgs e) { if (!Page.IsPostBack ) //推断页面是否第一次载入 { SqlConnection con = DB.createConnection(); //此方法在上一篇文章中已经介绍,调用一个已经编写好的创建数据库连接的方法。 SqlCommand cmd = new SqlCommand("select * from province",con); SqlDataReader sdr = cmd.ExecuteReader(); this.DropDownList1.DataTextField = "proName"; this.DropDownList1.DataValueField = "proID"; //主键字段 this.DropDownList1.DataSource = sdr; this.DropDownList1.DataBind(); sdr.Close(); } }</span>
编写DropDownList1_SelectedIndexChanged事件代码,实现单击“省”,ListBox自己主动加入该“省”所具有的“市”
<span style="font-family:KaiTi_GB2312;font-size:18px;"> protected void DropDownList1_SelectedIndexChanged(object sender, EventArgs e) { this.ListBox1.Items.Clear(); SqlConnection con2 = DB.createConnection(); SqlCommand cmd1 = new SqlCommand("select * from city where proID=" + this.DropDownList1.SelectedValue, con2); SqlDataReader sdr1 = cmd1.ExecuteReader(); while (sdr1.Read()) { this.ListBox1.Items.Add(new ListItem(sdr1.GetString(2),sdr1.GetInt32(0).ToString())); } }</span>执行文件,效果图例如以下所看到的
这里河北省的城市我没有加入完整,仅仅是为了实现两级联动的功能,相比前两篇博客中Web控件GridView和Repeater的使用,GridView和Repeater功能尽管是相当强大,可是不同的控件有不同的用途。在这里,杀鸡焉用牛刀?
相关文章推荐
- 《ASP.NET》数据绑定—DropDownList、ListBox
- ASP.NET 2.0的 GridView中模板列绑定DropDownList的数据处理
- Asp.Net--DropDownList与DataTable数据绑定的方法(C#)[学习笔记]
- ASP.net DropDownList数据绑定及使用详解
- Asp.net下用DropDownList绑定Xml数据
- [ASP.NET] 将数据绑定到DropDownlist中的常见问题
- 绑定access数据库,asp.net 中 以DropDownList为查询条件 ,前台实现数据查询
- ASP.NET中DropDownList控件的数据绑定的C#语言描述
- asp.net中dropdownlist中绑定数据…
- ASP.NET中 RadioButtonList(单选按钮组),CheckBoxList(复选框组),DropDownList(下拉框),ListBox(列表框),BulletedList(无序列表)的使用前台绑定
- asp.net ListBox 绑定数据
- ASP.NET dropdownlist绑定数据却显示System.Data.DataRowView
- asp.net读取Xml文件并进行DropDownList数据绑定
- ASP.NET中DropDownList控件的简单使用--DDL绑定数据库表中值,选择DDL中值直接检索数据
- ASP.NET MVC DropDownList数据绑定及使用详解
- ASP.NET 中 DropDownList+DataList(数据列表)的使用前台绑定
- ASP.NET中DropDownList下拉框列表控件绑定数据的4种方法
- ASP.NET中dropdownlist 动态绑定数据的二种方法
- ASP.NET中DropDownList控件的数据绑定的C#语言描述
- ASP.NET GridView 内嵌 DropDownList 绑定数据