zoukankan      html  css  js  c++  java
  • 实现dropDownList listBox联动

    在listBox中显示dropDownList中选定的部门

    一 SQL SERVER建立两张表
       1 建立数据库department
       2 在department中建立两张表:部门表 TDepartment(depID,depName)
                                   员工表 emp(empID,empName,depID)

    二 在VS下新建Web页
       1 拖入一个dropDownList和一个listBox,分别改名为ddlDep,lBoxEmp
       2 首先把数据库中的部门显示在dropDownList中
         2.1 添加数据库连接类 DB.cs
             using System;
             using System.Data.SqlClient;
             public class DB
             {
          public DB()
          {}
                 public static SqlConnection createCon()
                 {
                      return new SqlConnection("server=.;database=adoNetTest;uid=huan;pwd=1234567890");
                 }
             }

         2.2 编写Page_Load方法
             if(!this.IsPostBack)   
             {
                 SqlConnection con=DB.createCon();
                 con.Open();
                 SqlCommand cmd=new Sqlcommand("Select * from TDeparment");
                 SqlDataReader sdr=cmd.ExecuteReader();
                 this.ddlDep.DataSource=sdr;           //数据源
                 this.ddlDep.DataTextField="depName";  //指定要显示的列名
                 this.ddlDep.DataValueField="depID";  //主键
                 this.ddlDep.DataBind();              //绑定
                 sdr.Close();
             }
      3  在listBox中显示对应部门的员工
           3.1  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();
         
          3.2 修改ddlDep的AutoPostBack属性为true
              编写事件SelectedIndexChanged双击即可,添加如下代码
              this.lBoxEmp.Items.Clear();
              SqlConnection con=DB.createCon();
              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();
      4 关闭连接
              con.Close();

  • 相关阅读:
    apache bench(ab)压力测试模拟POSt请求
    使用pabot并发执行robotframework的testSuite
    Python实现简单的HTTP服务器(支持文件上传下载)
    Mac OS启动服务优化高级篇(launchd tuning)
    在MacOS下Python安装lxml报错xmlversion.h not found 报错的解决方案
    模拟恶劣网络环境常用的几种解决方案
    Web缓存基础:术语、HTTP报头和缓存策略
    RTMP直播应用与延时分析
    用Redis作为Mysql数据库的缓存【转】
    缓存雪崩,缓存穿透解决方案
  • 原文地址:https://www.cnblogs.com/chenlong/p/1590743.html
Copyright © 2011-2022 走看看