asp.net 有刷新实现三级联动

本文介绍了一种使用ASP.NET和SQL Server实现省、市、县三级联动下拉选择框的方法。通过从数据库中获取省市县数据并绑定到下拉框,当用户选择某个省份时,对应的市和县会自动更新。此方案利用了C#编程语言和ADO.NET进行数据访问。
[csharp]  view plain copy print ?
  1. <span style="font-size:18px;">using System;  
  2. using System.Collections.Generic;  
  3. using System.Linq;  
  4. using System.Web;  
  5. using System.Web.UI;  
  6. using System.Web.UI.WebControls;  
  7. using System.Data;  
  8. using System.Data.SqlClient;  
  9. using System.Configuration;  
  10.   
  11. public partial class _Default : System.Web.UI.Page  
  12. {  
  13.     string sqlstr=ConfigurationManager.ConnectionStrings["sqlstr"].ConnectionString;  
  14.     protected void Page_Load(object sender, EventArgs e)  
  15.     {  
  16.         if (!IsPostBack)  
  17.         {  
  18.             GetProcince();  
  19.             DropDownList4.Items.Insert(0, "--请选择省份--");  
  20.             DropDownList2.Items.Insert(0, "--请选择城市--");  
  21.             DropDownList3.Items.Insert(0, "--请选择区县--");  
  22.         }  
  23.           
  24.     }  
  25.     private void GetProcince()  
  26.     {  
  27.         using (SqlConnection sqlcnn=new SqlConnection(sqlstr))  
  28.         {  
  29.             using (SqlCommand sqlcmm=sqlcnn.CreateCommand())  
  30.             {  
  31.                 sqlcmm.CommandText = "select * from province";  
  32.                 sqlcnn.Open();  
  33.                 DataTable dt = new DataTable();  
  34.                 SqlDataAdapter adapter = new SqlDataAdapter(sqlcmm);  
  35.                 adapter.Fill(dt);  
  36.                 this.DropDownList4.DataSource = dt;  
  37.                 this.DropDownList4.DataTextField = "province";  
  38.                 this.DropDownList4.DataValueField = "provinceid";  
  39.                 this.DropDownList4.DataBind();  
  40.             }  
  41.         }  
  42.     }  
  43.     protected void DropDownList4_SelectedIndexChanged(object sender, EventArgs e)  
  44.     {  
  45.         this.DropDownList3.Items.Clear();  
  46.         using (SqlConnection sqlcnn = new SqlConnection(sqlstr))  
  47.         {  
  48.             using (SqlCommand sqlcmm = sqlcnn.CreateCommand())  
  49.             {  
  50.                 sqlcmm.CommandText = "select * from city where father=@father";  
  51.                 sqlcmm.Parameters.AddWithValue("@father"this.DropDownList4.SelectedValue);  
  52.                 sqlcnn.Open();  
  53.                 DataTable dt = new DataTable();  
  54.                 SqlDataAdapter adapter = new SqlDataAdapter(sqlcmm);  
  55.                 adapter.Fill(dt);  
  56.                 this.DropDownList2.DataSource = dt;  
  57.                 this.DropDownList2.DataTextField = "city";  
  58.                 this.DropDownList2.DataValueField = "cityID";  
  59.                 this.DropDownList2.DataBind();  
  60.                 DropDownList2.Items.Insert(0, "--请选择城市--");  
  61.                 DropDownList3.Items.Insert(0, "--请选择区县--");  
  62.             }  
  63.         }  
  64.     }  
  65.   
  66.     protected void DropDownList2_SelectedIndexChanged(object sender, EventArgs e)  
  67.     {  
  68.         using (SqlConnection sqlcnn = new SqlConnection(sqlstr))  
  69.         {  
  70.             using (SqlCommand sqlcmm = sqlcnn.CreateCommand())  
  71.             {  
  72.                 sqlcmm.CommandText = "select * from area where father=@father";  
  73.                 sqlcmm.Parameters.AddWithValue("@father"this.DropDownList2.SelectedValue);  
  74.                 sqlcnn.Open();  
  75.                 DataTable dt = new DataTable();  
  76.                 SqlDataAdapter adapter = new SqlDataAdapter(sqlcmm);  
  77.                 adapter.Fill(dt);  
  78.                 this.DropDownList3.DataSource = dt;  
  79.                 this.DropDownList3.DataTextField = "area";  
  80.                 this.DropDownList3.DataValueField = "areaID";  
  81.                 this.DropDownList3.DataBind();  
  82.                 DropDownList3.Items.Insert(0, "--请选择区县--");  
  83.             }  
  84.         }  
  85.     }  
  86. }  
  87.   
  88.    
  89.   
  90. </span>  
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值