dropdownlist 绑定 数据源

关于选中一个省份,下面相应的出现相关城市.
建一数据库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();
  }
 
 
早晨起来以为可以上交这份程序,出了错找不到原因,刚回来再一看少输入一个字母,浪费了我六点半至八点的时间.真是郁闷!不过也碰到了一些满有用处学习资料...
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值