文件名称:城市的连动效果
文件大小:983B
文件格式:SLN
更新时间:2016-05-14 14:26:59
Ctiy Dropdownlist
实现城市连动 using System; using System.Collections; using System.Configuration; using System.Data; using System.Linq; using System.Web; using System.Web.Security; using System.Web.UI; using System.Web.UI.HtmlControls; using System.Web.UI.WebControls; using System.Web.UI.WebControls.WebParts; using System.Xml.Linq; using System.Data.SqlClient; namespace _0327Province_City_代码实现_ { public partial class _Default : System.Web.UI.Page { protected void Page_Load(object sender, EventArgs e) { if (!Page.IsPostBack) { string connstring = @"Data Source=.\SQLExpress;Initial Catalog=IDInfoDB;Integrated Security=True";//@表示转义字符 .\sqlexpress表示数据源 SqlConnection sconn = new SqlConnection(connstring); string Tablename = "province"; string selectstring = "select * from " + Tablename; SqlDataAdapter sda = new SqlDataAdapter(selectstring, sconn); DataSet ds = new DataSet(); sconn.Open(); sda.Fill(ds, Tablename); sconn.Close(); this.ddl_Province .DataSource = ds.Tables[Tablename].DefaultView; this.ddl_Province .DataTextField = "province"; this.ddl_Province .DataValueField = "provinceID"; this.ddl_Province .DataBind(); } } protected void ddl_City_SelectedIndexChanged(object sender, EventArgs e) { if (this.ddl_City .SelectedIndex >= 0) { string connstring = @"Data Source=.\SQLExpress;Initial Catalog=IDInfoDB;Integrated Security=True";//@表示转义字符 .\sqlexpress表示数据源 SqlConnection sconn = new SqlConnection(connstring); string Tablename = "area"; string selectstring = "select * from " + Tablename + " where cityid='" + this.ddl_City .SelectedValue + "'"; SqlDataAdapter sda = new SqlDataAdapter(selectstring, sconn); DataSet ds = new DataSet(); sconn.Open(); sda.Fill(ds, Tablename); sconn.Close(); this.ddl_Area .DataSource = ds.Tables[Tablename].DefaultView; this.ddl_Area .DataTextField = "area"; this.ddl_Area .DataValueField = "areaID"; this.ddl_Area .DataBind(); } } protected void ddl_Province_SelectedIndexChanged(object sender, EventArgs e) { if (this.ddl_Province .SelectedIndex >= 0) { string connstring = @"Data Source=.\SQLExpress;Initial Catalog=IDInfoDB;Integrated Security=True";//@表示转义字符 .\sqlexpress表示数据源 SqlConnection sconn = new SqlConnection(connstring); string Tablename = "city"; string selectstring = "select * from " + Tablename + " where provinceid='" + this.ddl_Province .SelectedValue + "'"; SqlDataAdapter sda = new SqlDataAdapter(selectstring, sconn); DataSet ds = new DataSet(); sconn.Open(); sda.Fill(ds, Tablename); sconn.Close(); this.ddl_City .DataSource = ds.Tables[Tablename].DefaultView; this.ddl_City .DataTextField = "city"; this.ddl_City .DataValueField = "cityID"; this.ddl_City .DataBind(); } } } }