System.Web.UI.WebControls

  1. using System;  
  2. using System.Collections.Generic;  
  3. using System.Linq;  
  4. using System.Web;  
  5. using System.Web.UI;    http://www.gitom.com/973631
  6. using System.Web.UI.WebControls;  
  7. using System.Data.SqlClient;  
  8. using System.Data;  
  9.   
  10. namespace 省市联动  
  11. {  
  12.     public partial class 省市联动 : System.Web.UI.Page  
  13.     {  
  14.         string strCon = "server=.;database=Place;uid=sa;pwd=123456";  
  15.         protected void Page_Load(object sender, EventArgs e)  
  16.         {  
  17.             //DropDownList1.Text = "北京市";  
  18.             if (!IsPostBack)  
  19.             {  
  20.                  
  21.                 SqlConnection sqlcon = new SqlConnection(strCon);  
  22.                 string sqlstr = "select * from Province";  
  23.                 string strCity = "select * from City where Province = '北京市'";  
  24.                 SqlDataAdapter myda = new SqlDataAdapter(sqlstr, strCon);  
  25.                 SqlDataAdapter mydaCity = new SqlDataAdapter(strCity, sqlcon);  
  26.                 DataSet myds = new DataSet();  
  27.                 DataSet mydsCity = new DataSet();  
  28.                 sqlcon.Open();  
  29.                 myda.Fill(myds);  
  30.                 mydaCity.Fill(mydsCity);  
  31.                 DropDownList1.DataSource = myds;  
  32.                 DropDownList1.DataValueField = "ProvinceName";  
  33.                 DropDownList1.DataBind();  
  34.                 DropDownList2.DataSource = mydsCity;  
  35.                 DropDownList2.DataValueField = "CityName";  
  36.                 DropDownList2.DataBind();  
  37.                 sqlcon.Close();  
  38.             }  
  39.         }  
  40.   
  41.         protected void DropDownList1_SelectedIndexChanged(object sender, EventArgs e)  
  42.         {  
  43.             SqlConnection sqlcon = new SqlConnection(strCon);  
  44.             string sqlstr = "select * from City where Province = '" + DropDownList1.SelectedValue.Trim() + "'";  
  45.             SqlDataAdapter myda = new SqlDataAdapter(sqlstr, sqlcon);  
  46.             DataSet myds = new DataSet();  
  47.             sqlcon.Open();  
  48.             myda.Fill(myds);  
  49.             DropDownList2.DataSource = myds;  
  50.             DropDownList2.DataValueField = "CityName";  
  51.             DropDownList2.DataBind();  
  52.             sqlcon.Close();  http://www.gitom.com/973631
  53.         }  
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值