📄 webform1.aspx.cs
字号:
using System;
using System.Collections;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Web;
using System.Web.SessionState;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;
using System.Data.SqlClient;
namespace dropdown_listbox
{
/// <summary>
/// WebForm1 的摘要说明。
/// </summary>
public class WebForm1 : System.Web.UI.Page
{
protected System.Web.UI.WebControls.Label Label1;
protected System.Web.UI.WebControls.Label Label2;
protected System.Web.UI.WebControls.DropDownList DropDownList1;
protected System.Web.UI.WebControls.Label Label3;
protected System.Web.UI.WebControls.ListBox ListBox1;
private void Page_Load(object sender, System.EventArgs e)
{
// 在此处放置用户代码以初始化页面
if(!this.IsPostBack)
{
SqlConnection con=DB.createconnection();
con.Open();
SqlCommand cmd=new SqlCommand("select * from department",con);
SqlDataReader sdr=cmd.ExecuteReader();
this.DropDownList1.DataSource=sdr;
this.DropDownList1.DataTextField="departmentName";
this.DropDownList1.DataValueField="departmentID";
this.DropDownList1.DataBind();
sdr.Close();
SqlCommand cmd_employ=new SqlCommand("select * from employ where departmentID="+this.DropDownList1.SelectedValue,con);
SqlDataReader sdr_employ=cmd_employ.ExecuteReader();
// this.ListBox1.DataSource=sdr_employ;
// this.ListBox1.DataTextField="employName";
// this.ListBox1.DataValueField="employID";
// this.ListBox1.DataBind();
while(sdr_employ.Read())
{
this.ListBox1.Items.Add(new ListItem(sdr_employ.GetString(1),sdr_employ.GetInt32(0).ToString()));
}
sdr.Close();
con.Close();
}
}
#region Web 窗体设计器生成的代码
override protected void OnInit(EventArgs e)
{
//
// CODEGEN: 该调用是 ASP.NET Web 窗体设计器所必需的。
//
InitializeComponent();
base.OnInit(e);
}
/// <summary>
/// 设计器支持所需的方法 - 不要使用代码编辑器修改
/// 此方法的内容。
/// </summary>
private void InitializeComponent()
{
this.DropDownList1.SelectedIndexChanged += new System.EventHandler(this.DropDownList1_SelectedIndexChanged);
this.Load += new System.EventHandler(this.Page_Load);
}
#endregion
private void DropDownList1_SelectedIndexChanged(object sender, System.EventArgs e)
{
this.ListBox1.Items.Clear();
SqlConnection con=DB.createconnection();
con.Open();
SqlCommand cmd1=new SqlCommand("select * from employ where departmentID="+this.DropDownList1.SelectedValue,con);
SqlDataReader sdr1=cmd1.ExecuteReader();
while(sdr1.Read())
{
this.ListBox1.Items.Add(new ListItem(sdr1.GetString(1),sdr1.GetInt32(0).ToString()));
}
//
// this.ListBox1.DataSource=sdr;
// this.ListBox1.DataTextField="employName";
// this.ListBox1.DataValueField="employID";
// this.ListBox1.DataBind();
sdr1.Close();
con.Close();
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -