ASP.NET 省份选择器的问题。好纠结啊,纠结一个晚上了
自己构造了一个类
public class ProvinceItem { public int Id { get; set; } public string Name { get; set; } }
ProvinceItem item = new ProvinceItem(); item.Id = dataReader.GetInt32(dataReader.GetOrdinal("proID")); item.Name = dataReader.GetString(dataReader.GetOrdinal("proName")); cmbPro.Items.Add(item.Name);
using System;using System.Collections.Generic;using System.ComponentModel;using System.Data;using System.Drawing;using System.Linq;using System.Text;using System.Windows.Forms;using System.Data.SqlClient;using System.Configuration;namespace 省市选择{ public partial class Form1 : Form { public Form1() { InitializeComponent(); string connStr = ConfigurationManager.ConnectionStrings["DbConn"].ConnectionString; using (SqlConnection conn = new SqlConnection(connStr)) { conn.Open(); using (SqlCommand cmd = conn.CreateCommand()) { cmd.CommandText = "select * from promary"; using (SqlDataReader dataReader = cmd.ExecuteReader()) { while (dataReader.Read()) { ProvinceItem item = new ProvinceItem(); item.Id = dataReader.GetInt32(dataReader.GetOrdinal("proID")); item.Name = dataReader.GetString(dataReader.GetOrdinal("proName")); cmbPro.Items.Add(item); } } } } } public void cmbPro_SelectedIndexChanged(object sender, EventArgs e) { cmbCity.Items.Clear(); ProvinceItem pitem = (ProvinceItem) cmbPro.SelectedItem; string connStr = ConfigurationManager.ConnectionStrings["DbConn"].ConnectionString; using (SqlConnection conn = new SqlConnection(connStr)) { conn.Open(); using (SqlCommand cmd = conn.CreateCommand()) { cmd.CommandText = "select * from city where proID=@proID"; cmd.Parameters.Add(new SqlParameter("proID",pitem.Id)); using (SqlDataReader reader = cmd.ExecuteReader()) { while (reader.Read()) { string cityName = reader.GetString(reader.GetOrdinal("cityName")); cmbCity.Items.Add(cityName); } } } } } public class ProvinceItem { public int Id { get; set; } public string Name { get; set; } } } }