省市选择器

前台

 <form id="form1" runat="server">
    <div>
    <table>
   
    <tr>
    <td>
        <asp:DropDownList ID="Dropprovince" runat="server"
            onselectedindexchanged="Dropprovince_SelectedIndexChanged"
            AutoPostBack="True">
        </asp:DropDownList>
    </td>
    <td>
        <asp:DropDownList ID="Dropcity" runat="server" AutoPostBack="True"
            onselectedindexchanged="Dropcity_SelectedIndexChanged">
        </asp:DropDownList>
    </td>
    <td>
        <asp:DropDownList ID="Droparea" runat="server">
        </asp:DropDownList>
    </td>
    </tr>
    </table>
    </div>
    </form>

后台

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Configuration;
using System.Data.SqlClient;
using System.Data;

namespace 省市选择器
{
    public partial class WebForm1 : System.Web.UI.Page
    {
        string str = ConfigurationManager.ConnectionStrings["constr"].ConnectionString;
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
               
                SqlConnection con = new SqlConnection(str);
                SqlCommand cmd = con.CreateCommand();
                cmd.CommandText = "select * from province";
                DataTable dt = new DataTable();
                SqlDataAdapter adapter = new SqlDataAdapter(cmd);
                adapter.Fill(dt);
                Dropprovince.DataSource = dt;
                Dropprovince.DataTextField = "province";
                Dropprovince.DataValueField = "provinceID";
                Dropprovince.DataBind();
                Dropcity.Items.Add("--请选择市--");
                Droparea.Items.Add("--请选择地区--");
            }
          

        }

        protected void Dropprovince_SelectedIndexChanged(object sender, EventArgs e)
        {
            SqlConnection con= new SqlConnection(str);
            SqlCommand cmd = con.CreateCommand();
            cmd.CommandText = "select * from city where father=@father ";
            cmd.Parameters.AddWithValue("@father",Dropprovince.SelectedItem.Value);
            DataTable dt = new DataTable();
            SqlDataAdapter adapter = new SqlDataAdapter(cmd);
            adapter.Fill(dt);
            Dropcity.DataSource = dt;
            Dropcity.DataTextField = "city";
            Dropcity.DataValueField = "cityID";
            Dropcity.DataBind();
        }

        protected void Dropcity_SelectedIndexChanged(object sender, EventArgs e)
        {
            SqlConnection con = new SqlConnection(str);
            SqlCommand cmd = con.CreateCommand();
            cmd.CommandText = "select * from area where father=@father";
            cmd.Parameters.AddWithValue("@father", Dropcity.SelectedItem.Value);
            DataTable dt = new DataTable();
            SqlDataAdapter adapter = new SqlDataAdapter(cmd);
            adapter.Fill(dt);
            Droparea.DataSource = dt;
            Droparea.DataTextField = "area";
            Droparea.DataValueField = "areaID";
            Droparea.DataBind();
           
        }
    }
}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值