省市县只要一张表
SY_CITYCODE
一、前端
<%@ Control Language="C#" AutoEventWireup="true" CodeFile="PCAControl.ascx.cs" Inherits="PCAControl" %>
<asp:ScriptManager ID="ScriptManager1" runat="server"></asp:ScriptManager>
<asp:UpdatePanel ID="UpdatePanel1" runat="server">
<ContentTemplate>
<asp:DropDownList ID="ddlProvice" runat="server" AppendDataBoundItems="true" AutoPostBack="true"
OnSelectedIndexChanged="ddlProvice_SelectedIndexChanged">
<asp:ListItem Text="-请选择省份-" Value=""></asp:ListItem>
</asp:DropDownList>
<asp:DropDownList ID="ddlCity" runat="server" AutoPostBack="true"
onselectedindexchanged="ddlCity_SelectedIndexChanged">
<asp:ListItem Text="-请选择城市-" Value=""></asp:ListItem>
</asp:DropDownList>
<asp:DropDownList ID="ddlArea" runat="server">
<asp:ListItem Text="-请选择县区-" Value=""></asp:ListItem>
</asp:DropDownList>
</ContentTemplate>
</asp:UpdatePanel>
二、后置代码
//加载省份
protected void BindProvince()
{
DataTable dt = MSCL.sqlHelper.GetDataTable("SELECT DISTINCT PROVINCE FROM SY_CITYCODE GROUP BY PROVINCE");
ddlProvice.DataTextField = "PROVINCE";
ddlProvice.DataValueField = "PROVINCE";
ddlProvice.DataSource = dt;
ddlProvice.DataBind();
}
//加载城市
protected void ddlProvice_SelectedIndexChanged(object sender,EventArgs e)
{
string SelectPro = ddlProvice.SelectedValue;
if (!string.IsNullOrEmpty(SelectPro))
{
ddlCity.Items.Clear();
ddlCity.AppendDataBoundItems = true;
ddlCity.Items.Insert(0,new ListItem("-请选择城市-",""));
DataTable dt1 = MSCL.sqlHelper.GetDataTable("SELECT CITY FROM SY_CITYCODE Where PROVINCE='" + SelectPro + "' Group By City");
ddlCity.DataTextField = "CITY";
ddlCity.DataValueField = "CITY";
ddlCity.DataSource = dt1;
ddlCity.DataBind();
}
}
//加载县区
protected void ddlCity_SelectedIndexChanged(object sender,EventArgs e)
{
string SelectCity = ddlCity.SelectedValue;
if (!string.IsNullOrEmpty(SelectCity))
{
ddlArea.Items.Clear();
ddlArea.AppendDataBoundItems = true;
ddlArea.Items.Insert(0,new ListItem("-请选择县区-",""));
DataTable dt2 = MSCL.sqlHelper.GetDataTable("SELECT AREA FROM SY_CITYCODE Where CITY='" + SelectCity + "'");
ddlArea.DataTextField = "AREA";
ddlArea.DataValueField = "AREA";
ddlArea.DataSource = dt2;
ddlArea.DataBind();
}
}
string SelectedProv = ((DropDownList)PCAControl1.FindControl("ddlProvice")).SelectedValue; string SelectedCity = ((DropDownList)PCAControl1.FindControl("ddlCity")).SelectedValue; string SelectedArea = ((DropDownList)PCAControl1.FindControl("ddlArea")).SelectedValue;
原文链接:https://www.f2er.com/ajax/165428.html