简易下拉列表联动
using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Data.SqlClient;
public partial class _Default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
if (!Page.IsPostBack)
{
SqlConnection conn = DB.createConnection();
conn.Open();
//显示部门
SqlCommand cmd = new SqlCommand("SELECT * FROM TDepartment", conn);
SqlDataReader dr = cmd.ExecuteReader();
this.ddlDep.DataSource = dr;
this.ddlDep.DataTextField = "depName";
this.ddlDep.DataValueField = "depID";
this.DataBind();
dr.Close();
//显示员工
SqlCommand cmdEmp = new SqlCommand("SELECT * FROM emp WHERE depID="+this.ddlDep.SelectedValue, conn);
SqlDataReader drEmp = cmdEmp.ExecuteReader();
while (drEmp.Read())
{
this.lbEmp.Items.Add(new ListItem(drEmp.GetString(1), drEmp.GetInt32(0).ToString()));
}
drEmp.Close();
conn.Close();
conn.Dispose();
}
}
protected void ddlDep_SelectedIndexChanged(object sender, EventArgs e)
{
this.lbEmp.Items.Clear();
SqlConnection conn = DB.createConnection();
conn.Open();
SqlCommand cmdEmp = new SqlCommand("SELECT * FROM emp WHERE depID="+this.ddlDep.SelectedValue, conn);
SqlDataReader drEmp = cmdEmp.ExecuteReader();
while (drEmp.Read())
{
this.lbEmp.Items.Add(new ListItem(drEmp.GetString(1), drEmp.GetInt32(0).ToString()));
}
drEmp.Close();
conn.Close();
conn.Dispose();
}
}