protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
SqlConnection con = new SqlConnection("server=;database=province;uid=sa;pwd=sa;");
con.Open();
SqlCommand cmd = new SqlCommand("select * from province", con);
SqlDataReader sdr = cmd.ExecuteReader();
DropDownList1.DataSource = sdr;
DropDownList1.DataTextField = "proname";
DropDownList1.DataValueField = "proid";
DropDownList1.DataBind();
con.Close();
sdr.Close();
}
}
protected void DropDownList1_SelectedIndexChanged(object sender, EventArgs e) //选择第一个DropDownList中的项后后数据绑定
{
SqlConnection con = new SqlConnection("server=;database=province;uid=sa;pwd=sa;");
con.Open();
SqlCommand cmd2 = new SqlCommand("select * from city where proid=" + DropDownList1.SelectedValue, con);
SqlDataReader sdr2 = cmd2.ExecuteReader();
DropDownList2.DataSource = sdr2;
DropDownList2.DataTextField = "cityname";
DropDownList2.DataValueField = "cityid";
DropDownList2.DataBind();
DropDownList2.Visible = true;
sdr2.Close();
con.Close();
}
再例子
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
DataSet ds = onkhriss.Labor.findBuMen();
this.DropDownList1.DataSource = ds.Tables[0];
this.DropDownList1.DataTextField = "d_name";
this.DropDownList1.DataValueField = "d_id";
this.DropDownList1.DataBind();
if (DropDownList2.Text == "")
{
int d_id = Convert.ToInt32(this.DropDownList1.SelectedValue);
DataSet ds2 = onkhriss.Labor.findUser(d_id);
this.DropDownList2.DataSource = ds2.Tables[0];
this.DropDownList2.DataTextField = "a_username";
this.DropDownList2.DataValueField = "a_did";
this.DropDownList2.DataBind();
}
}
}
protected void DropDownList1_SelectedIndexChanged(object sender, EventArgs e)
{
int d_id = Convert.ToInt32(this.DropDownList1.SelectedValue);
DataSet ds2 = onkhriss.Labor.findUser(d_id);
this.DropDownList2.DataSource = ds2.Tables[0];
this.DropDownList2.DataTextField = "a_username";
this.DropDownList2.DataValueField = "a_did";
this.DropDownList2.DataBind();
}
//查询部门
public static DataSet findBuMen()
{
return (DataSet)db.ExecuteDataSet(CommandType.Text, "select * from tb_department");
}
//查询用户
public static DataSet findUser(int id)
{
return (DataSet)db.ExecuteDataSet(CommandType.Text, "select * from tb_AdminUser where a_did=" + id);
}
http://topic.csdn.net/u/20090615/22/40716d78-78c7-4d65-a9fa-78925f3c5e80.html