<%@ Page Language="C#" AutoEventWireup="true" CodeFile="listbox.aspx.cs" Inherits="test_listbox" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
<title>无标题页</title>
</head>
<body>
<form ;
this.DropDownList1.DataBind();
sdr.Close();
//con.Close();
SqlCommand cmdCity =new SqlCommand("select * from city where proID=" + this.DropDownList1.SelectedValue.ToString(),con);
sdr = cmdCity.ExecuteReader();
this.DropDownList2.DataSource = sdr;
this.DropDownList2.DataValueField = "cityID";
this.DropDownList2.DataTextField = "cityName";
this.DropDownList2.DataBind();
sdr.Close();
con.Close();
}
}
protected void Button1_Click(object sender, EventArgs e)
{
this.ListBox1.Items.Add(this.TextBox1.Text);
}
protected void ListBox1_SelectedIndexChanged(object sender, EventArgs e)
{
Label1.Text = "你已经选择了:" + this.ListBox1.SelectedItem.Text;
}
protected void DropDownList1_SelectedIndexChanged(object sender, EventArgs e)
{
SqlConnection con = DB2.createConnection();
con.Open();
SqlCommand cmd = new SqlCommand("select * from city where proID=" + this.DropDownList1.SelectedValue.ToString(), con);
SqlDataReader sdr = cmd.ExecuteReader();
this.DropDownList2.DataSource = sdr;
this.DropDownList2.DataValueField = "cityID";
this.DropDownList2.DataTextField = "cityName";
this.DropDownList2.DataBind();
sdr.Close();
con.Close();
}
}
下面一节课是: DropDownList 与 ListBox 的联动!