一座城池

成功就是不懈的努力加上一点点机会,努力是加法,机会是乘法,两者兼备才会得到最大的结果!
当前位置 - 首页 - ASP.NET - 省市联动(ASP.NET + XML)

省市联动(ASP.NET + XML)

2014/5/20 18:04:37  发表于:2014/5/20 18:04:37  ASP.NET 次浏览 
字体大小切换:

方法中的XML文件见附件!Province.xml


前台代码:
 
<asp:DropDownList ID="DropDownListProvince" runat="server" CssClass="input select" AutoPostBack="True" OnSelectedIndexChanged="DropDownListProvince_SelectedIndexChanged"></asp:DropDownList>
<asp:DropDownList ID="DropDownListCity" runat="server" CssClass="input select"></asp:DropDownList><span style="color:red;">*</span>
<asp:Label ID="LabelMessage" runat="server" Text=""></asp:Label>

后台代码:
using System;
using System.Collections.Generic;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Xml;
namespace DTcms.Web
{
    public partial class join : System.Web.UI.Page
    {
        public string City = "广州";
        protected void Page_Load(object sender, EventArgs e)
        {
            // 在此处放置用户代码以初始化页面
            string CurrentPath = this.Server.MapPath(".");
            if (!Page.IsPostBack)
            {
                if (System.IO.File.Exists(CurrentPath + "\\Province.xml"))
                {
                    this.DropDownListProvince.Items.Clear();
                    System.Xml.XmlDocument doc = new System.Xml.XmlDocument();
                    doc.Load(CurrentPath + "\\Province.xml");
                    XmlNodeList nodes = doc.DocumentElement.ChildNodes;
                    XmlNode node1 = doc.DocumentElement.SelectSingleNode(@"Province/City[@Name='" + this.City + "']");
                    foreach (XmlNode node in nodes)
                    {
                        this.DropDownListProvince.Items.Add(node.Attributes["Name"].Value);
                        int n = this.DropDownListProvince.Items.Count - 1;
                        if (node1 != null && node == node1.ParentNode)
                            this.DropDownListProvince.SelectedIndex = n;
                    }
                    DropDownListProvince_SelectedIndexChanged(sender, e);
                }
                else
                {
                    this.LabelMessage.Text = "地市信息文件丢失!";
                }
            }
        }
        protected void DropDownListProvince_SelectedIndexChanged(object sender, EventArgs e)
        {
            string CurrentPath = this.Server.MapPath(".");
            if (System.IO.File.Exists(CurrentPath + "\\Province.xml"))
            {
                this.DropDownListCity.Items.Clear();
                System.Xml.XmlDocument doc = new System.Xml.XmlDocument();
                doc.Load(CurrentPath + "\\Province.xml");
                XmlNodeList nodes = doc.DocumentElement.ChildNodes[this.DropDownListProvince.SelectedIndex].ChildNodes;
                foreach (XmlNode node in nodes)
                {
                    this.DropDownListCity.Items.Add(node.Attributes["Name"].Value);
                    int n = this.DropDownListCity.Items.Count - 1;
                    if (node.Attributes["Name"].Value == this.City)
                    {
                        this.DropDownListCity.SelectedIndex = n;
                    }
                }
                if (this.DropDownListCity.SelectedIndex == -1)
                    this.DropDownListCity.SelectedIndex = 0;
            }
            else
            {
                this.LabelMessage.Text = "地市信息文件丢失!";
            }
        }
    }
}

上一篇::亲,已经是第一篇了!
Top