就是说,两个下拉列表框,一个省份,一个城市选择省份,自动出来该省份的城市大概方法我知道,但是不想麻烦写,请高人给出源码。要省份和城市都写好的谢谢

解决方案 »

  1.   

    前台代码:
      <div>
            省<asp:DropDownList ID="DropDownList1" runat="server" AutoPostBack="true" OnSelectedIndexChanged="DropDownList1_SelectedIndexChanged">
            </asp:DropDownList>
            市<asp:DropDownList ID="DropDownList2" runat="server">
            </asp:DropDownList>
            </div>后台代码:
     protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                bind();
               // ShiBind();
            }
        }
        public void bind()//查找省
        {
            SqlConnection cn = new SqlConnection("server=localhost;database=student;trusted_connection=true");
            SqlCommand cmd = new SqlCommand("select * from sheng",cn);
            DataTable dt = new DataTable();
            SqlDataAdapter da = new SqlDataAdapter(cmd);
            da.Fill(dt);
            this.DropDownList1.DataTextField = "name";
            this.DropDownList1.DataValueField = "id";//获取多个表之间共同的字段
            this.DropDownList1.DataSource = dt.DefaultView;
            this.DropDownList1.DataBind();
        }
        protected void DropDownList1_SelectedIndexChanged(object sender, EventArgs e)//省级
        {
            DataTable dt = binds(Convert.ToInt32(DropDownList1.SelectedValue));
            this.DropDownList2.DataSource = dt;
            this.DropDownList2.DataTextField = "name";
            this.DropDownList2.DataBind();
        }
        public DataTable binds(int Id)//通过省市的ID作比较
        {
            SqlConnection cn = new SqlConnection("server=localhost;database=student;trusted_connection=true");
            string sql = string.Format("select name from shi where ShengID='{0}'",Id);
            SqlCommand cmd = new SqlCommand(sql,cn);
            DataTable dt = new DataTable();
            SqlDataAdapter da = new SqlDataAdapter(cmd);
            da.Fill(dt);
            return dt;
        }