ASP.NET实现省市区三级联动(局部刷新)

时间:2023-01-22 03:01:28

跟前一篇ASP.NET实现年月日三级联动(局部刷新)一样,没什么技术含量,直接上代码

<asp:ScriptManager ID="ScriptManager1" runat="server">
</asp:ScriptManager>
<asp:UpdatePanel ID="UpdatePanel1" runat="server">
<ContentTemplate>
<asp:DropDownList ID="ddl_Province" runat="server" AutoPostBack="True" OnSelectedIndexChanged="ddl_Province_SelectedIndexChanged">
</asp:DropDownList>
<asp:DropDownList ID="ddl_City" runat="server" AutoPostBack="True" OnSelectedIndexChanged="ddl_City_SelectedIndexChanged">
</asp:DropDownList>
<asp:DropDownList ID="ddl_Area" runat="server">
</asp:DropDownList>
</ContentTemplate>
</asp:UpdatePanel>
protected void Page_Load(object sender, EventArgs e)
{
if (!Page.IsPostBack)
{
BindProvince();
BindCity(ddl_Province.SelectedItem.Value);
BindArea(ddl_City.SelectedItem.Value);
}
} #region 三级省市区联动xml
private void BindProvince()
{
XmlDocument xmlDoc = new XmlDocument();
xmlDoc.Load(Server.MapPath("ChinaArea.xml"));
XmlNodeList nodelist = xmlDoc.SelectNodes("area/province[@provinceID]");
ListItem li = null;
foreach (XmlNode myNode in nodelist)
{
li = new ListItem();
li.Value = myNode.Attributes["provinceID"].Value;
li.Text = myNode.Attributes["province"].Value;
this.ddl_Province.Items.Add(li);
}
}
private void BindCity(string code)
{
XmlDocument xmlDoc = new XmlDocument();
xmlDoc.Load(Server.MapPath("ChinaArea.xml"));
XmlNodeList nodelist = xmlDoc.SelectNodes(string.Format("area/province[@provinceID={0}]/City",code));
ListItem li = null;
foreach (XmlNode myNode in nodelist)
{
li = new ListItem();
li.Value = myNode.Attributes["CityID"].Value;
li.Text = myNode.Attributes["City"].Value;
this.ddl_City.Items.Add(li);
}
} private void BindArea(string code)
{
XmlDocument xmlDoc = new XmlDocument();
xmlDoc.Load(Server.MapPath("ChinaArea.xml"));
XmlNodeList nodelist = xmlDoc.SelectNodes(string.Format("area/province/City[@CityID={0}]/Piecearea",code));
ListItem li = null;
foreach (XmlNode myNode in nodelist)
{
li = new ListItem();
li.Value = myNode.Attributes["PieceareaID"].Value;
li.Text = myNode.Attributes["Piecearea"].Value;
this.ddl_Area.Items.Add(li);
}
} protected void ddl_City_SelectedIndexChanged(object sender, EventArgs e)
{
ddl_Area.Items.Clear();
BindArea(ddl_City.SelectedItem.Value);
} protected void ddl_Province_SelectedIndexChanged(object sender, EventArgs e)
{
ddl_City.Items.Clear();
ddl_Area.Items.Clear();
BindCity(ddl_Province.SelectedItem.Value);
BindArea(ddl_City.SelectedItem.Value);
}
#endregion

http://www.res.isofteam.com/xml/area.txt

将连接中的文件保存到ChinaArea.xml中