《ASP.NET》数据绑定—DropDownList、ListBox
来源:互联网 发布:linux如何给文件夹改名 编辑:程序博客网 时间:2024/05/21 19:36
DropDownList和ListBox实现两级联动功能,他们也可以将从后台数据库中搜选的出来的信息加以绑定,这里要实现的功能是在DropDownList中选择“省”,然后让ListBox自动将其省份下的“市”显示出来,这就是所谓的两级联动功能,这个功能我们在很多注册网页上看见,今天咱们就用ASP.NET解开其神秘的面纱。
一、设置前台界面,在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功能虽然是相当强大,但是不同的控件有不同的用途,在这里,杀鸡焉用牛刀?
2 0
- 《ASP.NET》数据绑定—DropDownList、ListBox
- asp.net ListBox 绑定数据
- ASP.NET MVC 八 DropDownList数据绑定
- Asp.net—DropDownList与listbox的联动
- asp.net (C#)DropDownList从不同数据库进行数据绑定
- Asp.net下用DropDownList绑定Xml数据
- asp.net 读取Xml文件并进行DropDownList数据绑定
- ASP.NET GridView 内嵌 DropDownList 绑定数据
- asp.net中dropdownlist中绑定数据…
- [Asp.Net]4种方法把数据绑定到Dropdownlist
- [ASP.NET]利用DataTable对DropDownList进行数据绑定
- ASP.net DropDownList数据绑定及使用详解
- asp.net无限分类绑定DropDownList
- asp.net的DropDownList通用绑定代码
- asp.net mvc 绑定DropDownList或select
- asp.net mvc Dropdownlist的使用绑定
- 分享ASP.NET视频系列教程——第六讲 DropDownList,ListBox等控件
- Intelligent DropDownList and ListBox ASP.NET Server Controls
- C# PPT Operator
- OBJ-C异常处理大全
- !codeforces 400E Inna and Binary Logic-yy-(位运算)
- Django+MongoDB
- UVa 10315 - Poker Hands
- 《ASP.NET》数据绑定—DropDownList、ListBox
- JAVA应用程序打包成windows下的可执行程序的方法
- uva 705
- jQuery(一)
- 黑马程序员——Java基础---面向对象3
- 1011. World Cup Betting (20)
- sdnu山东省ACM 2010年第一届省赛Greatest Number
- 2015年多校联合训练第一场OO’s Sequence(hdu5288)
- LintCode解题笔记 - 连续子数组求和