ASP.NET实现级联下拉框效果实例讲解

所属分类: 网络编程 / ASP.NET 阅读数: 598
收藏 0 赞 0 分享

用ASP.NET控件实现部门和员工的联动,参考过程如下

效果图: 

Default.aspx代码:

<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %> 
 
<!DOCTYPE html> 
 
<html xmlns="http://www.w3.org/1999/xhtml"> 
<head runat="server"> 
<meta http-equiv="Content-Type" content="text/html; charset=utf-8"/> 
 <title></title> 
</head> 
<body> 
 <form id="form1" runat="server"> 
 <div> 
 
 <asp:DropDownList ID="ddlDep" runat="server" AutoPostBack="True" OnSelectedIndexChanged="ddlDep_SelectedIndexChanged"> 
 </asp:DropDownList> 
 <br /> 
 <asp:ListBox ID="lBoxEmp" runat="server"></asp:ListBox> 
 
 </div> 
 </form> 
</body> 
</html> 

Default.aspx.cs代码:

using System; 
using System.Collections.Generic; 
using System.Linq; 
using System.Web; 
using System.Web.UI; 
using System.Web.UI.WebControls; 
using System.Data.SqlClient; 
 
public partial class _Default : System.Web.UI.Page 
{ 
 protected void Page_Load(object sender, EventArgs e) 
 { 
 if (!this.IsPostBack) 
 { 
 SqlConnection con = DBCon.createConnection(); 
 con.Open(); 
 //显示部门 
 SqlCommand cmd = new SqlCommand("select * from Tdepartment", con); 
 SqlDataReader sdr = cmd.ExecuteReader(); 
 this.ddlDep.DataSource = sdr; 
 this.ddlDep.DataTextField = "depName"; 
 this.ddlDep.DataValueField = "depID"; 
 this.ddlDep.DataBind(); 
 sdr.Close(); 
 //显示员工 
 SqlCommand cmdEmp =new SqlCommand ("select * from emp where depID=" + this.ddlDep .SelectedValue ,con); 
 SqlDataReader sdrEmp = cmdEmp.ExecuteReader(); 
 while (sdrEmp.Read()) 
 { 
 this.lBoxEmp.Items.Add (new ListItem(sdrEmp.GetString(1),sdrEmp .GetInt32 (0).ToString ())); 
 } 
 sdrEmp.Close(); 
 //关闭连接 
 con.Close(); 
 } 
 } 
 protected void ddlDep_SelectedIndexChanged(object sender, EventArgs e) 
 { 
 this.lBoxEmp.Items.Clear(); 
 SqlConnection con = DBCon.createConnection(); 
 con.Open(); 
 SqlCommand cmdEmp = new SqlCommand("select * from emp where depID=" + this.ddlDep.SelectedValue, con); 
 SqlDataReader sdrEmp = cmdEmp.ExecuteReader(); 
 while (sdrEmp.Read()) 
 { 
 this.lBoxEmp.Items.Add(new ListItem(sdrEmp.GetString(1), sdrEmp.GetInt32(0).ToString())); 
 } 
 sdrEmp.Close(); 
 //关闭连接 
 con.Close(); 
 } 
} 

DBCon.cs代码

using System; 
using System.Collections.Generic; 
using System.Linq; 
using System.Web; 
using System.Data.SqlClient; 
 
/// <summary> 
/// DBCon 的摘要说明 
/// </summary> 
public class DBCon 
{ 
 public DBCon() 
 { 
 // 
 // TODO: 在此处添加构造函数逻辑 
 // 
 } 
 public static SqlConnection createConnection() 
 { 
 SqlConnection con = new SqlConnection("server=.;database=department;uid=sa;pwd=123456"); 
 return con; 
 } 
} 

使用Asp.net控件实现比较简单,但在大量用户使用的情况下最好不要使用,不断向服务器请求会给服务器带来很大的负担。使用JQuery和ajax实现可以有动态效果,实现过程比较复杂,但有数据缓冲和ajax局部刷新可以减少服务器的负担,JQuery实现级联下拉框效果,参考这篇文章://www.jb51.net/article/72366.htm

如果大家还想深入学习,可以点击jquery下拉框效果汇总JavaScript下拉框效果汇总进行学习。

以上就是ASP.NET实现级联下拉框效果实例讲解,希望大家可以学以致用。

更多精彩内容其他人还在看

ASP.Net 之Datalist删除功能详解附代码

ASP.Net 之Datalist删除功能详解附代码,需要的朋友可以参考一下
收藏 0 赞 0 分享

ASP.NET(C#)验证数字的两种方法

ASP.NET(C#)验证数字的两种方法,需要的朋友可以参考一下
收藏 0 赞 0 分享

此页的状态信息无效,可能已损坏 的处理办法及原因分析

此页的状态信息无效,可能已损坏 的处理办法及原因分析,需要的朋友可以参考一下
收藏 0 赞 0 分享

MultiLine 换行后实现读取不换行的具体思路

输入内容中有换行,保存到数据库,直接查看感觉没有换行,但查询结果“以文本格式显示结果”你就会发现 其实是有换行的,下面与大家分享下具体的解决方法
收藏 0 赞 0 分享

swfupload ajax无刷新上传图片实例代码

在这里上传图片就需要用到ajax无刷新上传图片,这里面包含的东西不是一点半点。这里用到的是一个插件swfupload实现无刷新上传图片,感兴趣的朋友可以参考下哈
收藏 0 赞 0 分享

静态gb2312编码在项目传值出现中文乱码现象

参考的美工静态页面是gb2312格式的,当此编码拿到项目中后,utf-8编码的系统,加载页面时,会出现样式问题,比如不能正常居中等
收藏 0 赞 0 分享

System.Timers.Timer定时执行程序示例代码

如果是某个逻辑功能的定时,可以将code放到逻辑功能的类的静态构造函数中,在该逻辑类第一次执行时,静态构造函数会被调用,则定时自然启动
收藏 0 赞 0 分享

分享下Asp.Net面试题目及答案集合

这篇文章主要是总结asp.net开发人员在面试过程中常遇到的一些问题小结,需要的朋友可以参考下
收藏 0 赞 0 分享

给自定义Web控件添加事件(前后台代码)

给自定义控件(Web Control)添加事件具体前后台代码如下,感兴趣的朋友可以参考下哈
收藏 0 赞 0 分享

ASP.NET过滤器的应用方法介绍

ASP.NET过滤器的应用方法介绍,需要的朋友可以参考一下
收藏 0 赞 0 分享
查看更多