关于选中一个省份,下面相应的出现相关城市.
建一数据库DDL:
表一Pro:ProID,ProName;表二City:CityID,ProID,CityName.
插入一三行两列的表格,属性border值设为O;在表格中添加两个控件DropDownList,第一个DropDownList的AutoPostBack属性设为ture;还添加一个TextBox,属性textmode设为password.
窗体中代码如下:
private void Page_Load(object sender, System.EventArgs e)
{
this.TextBox1.Attributes.Add("value",Request["TextBox1"]);
if(!this.IsPostBack)
{
OleDbConnection con =DB.CreateCon();
con.Open();
OleDbCommand cmd=new OleDbCommand("select * from Pro",con);
OleDbDataReader dr=cmd.ExecuteReader();
this.DropDownList1.DataSource=dr;
this.DropDownList1.DataTextField="ProName";
this.DropDownList1.DataValueField="ProID";
this.DropDownList1.DataBind();
dr.Close();
OleDbCommand cmd1=new OleDbCommand("select * from City where ProID='"+this.DropDownList1.SelectedValue+"'",con);
OleDbDataReader dr1=cmd1.ExecuteReader();
this.DropDownList2.DataSource=dr1;
this.DropDownList2.DataTextField="CityName";
this.DropDownList2.DataValueField="CityID";
this.DropDownList2.DataBind();
con.Close();
}
}
private void DropDownList1_SelectedIndexChanged(object sender, System.EventArgs e)
{
OleDbConnection con =DB.CreateCon();
con.Open();
OleDbCommand cmd=new OleDbCommand("select * from City where ProID='"+this.DropDownList1.SelectedValue+"'",con);
OleDbDataReader dr=cmd.ExecuteReader();
this.DropDownList2.DataSource=dr;
this.DropDownList2.DataTextField="CityName";
this.DropDownList2.DataValueField="CityID";
this.DropDownList2.DataBind();
con.Close();
早晨起来以为可以上交这份程序,出了错找不到原因,刚回来再一看少输入一个字母,浪费了我六点半至八点的时间.真是郁闷!不过也碰到了一些满有用处学习资料...